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.
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
IDs of the files to move to the Media Manager's trash bin.
Options to use when deleting files.
Returns
Return Type:
Was this helpful?
1import { files } from 'wix-media.v2';23/* Sample fileIds value:4 * [5 * 'w8ide0_v12i2pi4549locqdfeb5yy5b8iyh39az.pdf',6 * 'w8ide0_ye3x8yyf5izwe01ovn682pa76bzrrcyt.pdf'7 * ]8 *9 * Sample options value:10 * {11 * permanent: true12 * }13 */1415async function myBulkDeleteFilesFunction(fileIds, options) {16 try {17 await files.bulkDeleteFiles(fileIds, options);1819 console.log('Permanently deleted files.');20 return;21 } catch (error) {22 console.error(error);23 // Handle the error24 }25}2627/* Promise resolves to void */28
1import { Permissions, webMethod } from 'wix-web-module';2import { files } from 'wix-media.v2';3import { elevate } from 'wix-auth';45/* Sample fileIds value:6 * [7 * 'w8ide0_v12i2pi4549locqdfeb5yy5b8iyh39az.pdf',8 * 'w8ide0_ye3x8yyf5izwe01ovn682pa76bzrrcyt.pdf'9 * ]10 *11 * Sample options value:12 * {13 * permanent: true14 * }15 */1617export const myBulkDeleteFilesFunction = webMethod(Permissions.Anyone, async (fileIds, options) => {18 try {19 const elevatedBulkDeleteFiles = elevate(files.bulkDeleteFiles);20 await elevatedBulkDeleteFiles(fileIds, options);2122 console.log('Permanently deleted files.');23 return;24 } catch (error) {25 console.error(error);26 // Handle the error27 }28});2930/* Promise resolves to void */
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.
1/*******************************************2 * Backend code - bulk-delete-files.web.js *3 ******************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { files } from 'wix-business-tools.v2';7import { elevate } from 'wix-auth';89export const deleteFiles = webMethod(Permissions.Anyone, async (fileIds, options) => {10 try {11 const elevatedBulkDeleteFiles = elevate(files.bulkDeleteFiles);12 await elevatedBulkDeleteFiles(fileIds, options);1314 console.log(`Permanently deleted files with ids: ${fileIds.toString()}.`);15 return true;16 } catch (error) {17 console.error(error);18 }19});2021export 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);2627 const fileIds = returnedFiles.map((file) => {28 return file._id;29 });3031 return fileIds;32 } catch (error) {33 console.error(error);34 }35});363738/*************39 * Page code *40 ************/4142import { deleteFiles, searchFileIds } from 'backend/bulk-delete-files.web';4344$w.onReady(() => {45 $w('#delete').onClick(async () => {46 const isDeletePermanently = $w('#deletePermanently').checked;47 const options = { permanent: isDeletePermanently };4849 const parentFolder = $w('#parentFolder').value;50 const fileIds = await searchFileIds(parentFolder);5152 await deleteFiles(fileIds, options);53 $w('#successMessage').show();54 });55});56