Search.../

bulkDeleteFiles( )

Developer Preview

Deletes the specified files from the Media Manager.

Description

The bulkDeleteFiles() function returns a Promise that resolves when the files are deleted.

The deleted files are moved to the Media Manager's trash bin (TRASH_ROOT folder) unless permanently deleted. To permanently delete files, pass the permanent parameter with the value true. Permanently deleting files isn't reversible, so make sure that these files aren't being used in a site or in any other way as the files will no longer be accessible.

Notes:

  • The specified files can be from different folders.
  • Moving multiple files at once is an asynchronous action, and may take time for the changes to appear in the Media Manager.
  • Attempting to delete files that are already in the trash bin doesn't result in an error.
  • If your site contains deleted media files, the deleted media files still appear on your site as the files are still in the Media Manager (in the trash bin).
  • You can use bulkRestoreFilesFromTrashBin() to restore files from the Media Manager's trash bin.
Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function bulkDeleteFiles(fileIds: Array<string>, options: BulkDeleteFilesOptions): Promise<void>

bulkDeleteFiles Parameters

NAME
TYPE
DESCRIPTION
fileIds
Array<
string
>

IDs of the files to move to the Media Manager's trash bin.

options
Optional
BulkDeleteFilesOptions

Options to use when deleting files.

Returns

Return Type:

Promise<
void
>

Was this helpful?

Bulk delete files

Copy Code
1import { files } from 'wix-media.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample fileIds value:
5 * [
6 * 'w8ide0_v12i2pi4549locqdfeb5yy5b8iyh39az.pdf',
7 * 'w8ide0_ye3x8yyf5izwe01ovn682pa76bzrrcyt.pdf'
8 * ]
9 *
10 * Sample options value:
11 * {
12 * permanent: true
13 * }
14 */
15
16async function myBulkDeleteFilesFunction(fileIds, options) {
17 try {
18 const elevatedBulkDeleteFiles = elevate(files.bulkDeleteFiles);
19 await elevatedBulkDeleteFiles(fileIds, options);
20
21 console.log('Permanently deleted files.');
22 return;
23 } catch (error) {
24 console.error(error);
25 // Handle the error
26 }
27}
28
29/* Promise resolves to void */
Bulk delete all files found in a chosen folder

This code shows a page where the visitor chooses a folder by typing the folder's name in an input box, and then all the files in the chosen folder are deleted. Only visitors with permission to manage the site's media would have access to this page.

Copy Code
1/****************************************
2 * Backend code - bulk-delete-files.jsw *
3 ***************************************/
4
5import { files } from 'wix-business-tools.v2';
6import { elevate } from 'wix-auth';
7
8export async function deleteFiles (fileIds, options) {
9 try {
10 const elevatedBulkDeleteFiles = elevate(files.bulkDeleteFiles)
11 await elevatedBulkDeleteFiles(fileIds, options);
12
13 console.log(`Permanently deleted files with ids: ${fileIds.toString()}.`);
14 return true;
15 } catch (error) {
16 console.error(error);
17 }
18}
19
20export async function searchFileIds (parentFolderId) {
21 try {
22 const options = { parentFolder: parentFolderId}
23 const elevatedSearchFiles = elevate(files.searchFiles)
24 const returnedFiles = await elevatedSearchFiles(options);
25
26 const fileIds = returnedFiles.map((file) => {
27 return file._id
28 });
29
30 return fileIds;
31 } catch (error) {
32 console.error(error);
33 }
34}
35
36
37/*************
38 * Page code *
39 ************/
40
41import { deleteFiles, searchFileIds } from 'backend/bulk-delete-files';
42
43$w.onReady(() => {
44 $w('#delete').onClick(async () => {
45 const isDeletePermanently = $w('#deletePermanently').checked;
46 const options = {permanent: isDeletePermanently};
47
48 const parentFolder = $w('#parentFolder').value;
49 const fileIds = await searchFileIds(parentFolder);
50
51 await deleteFiles(fileIds, options);
52 $w('#successMessage').show();
53 });
54});