Search.../

bulkDeleteFiles( )

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 (dashboard page code)

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

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