Remove files
Files can be removed from a storage bucket using the remove API. If a file is protected by an identity Id, only the user who owns the file will be able to remove it.
Remove a single file
You can also perform a remove operation from a specific bucket by providing the target bucket's assigned name from Amplify Backend in bucket option.
import { remove } from 'aws-amplify/storage';
try { await remove({ path: 'album/2024/1.jpg', // Alternatively, path: ({identityId}) => `album/${identityId}/1.jpg` bucket: 'assignedNameInAmplifyBackend', // Specify a target bucket using name assigned in Amplify Backend });} catch (error) { console.log('Error ', error);}Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
import { remove } from 'aws-amplify/storage';
try { await remove({ path: 'album/2024/1.jpg', // Alternatively, provide bucket name from console and associated region bucket: { bucketName: 'bucket-name-from-console', region: 'us-east-2' }
});} catch (error) { console.log('Error ', error);}Remove folders
You can remove entire folders and their contents by providing a folder path. The remove API will automatically detect folders and perform batch deletion of all contained files.
import { remove } from 'aws-amplify/storage';
try { await remove({ path: 'album/2024/' });} catch (error) { console.error(error);}Folder deletion with progress tracking
For large folders, you can track the deletion progress and handle any failures:
import { remove } from 'aws-amplify/storage';
try { const result = await remove({ path: 'large-folder/', options: { onProgress: (fileBatch) => { console.log(fileBatch); } } }); console.log('Success', result);} catch (error) { console.log('Error during folder deletion:', error);}Cancellable folder deletion
You can cancel folder deletion operations, useful for user-initiated cancellations or when navigating away from a page.
When you call remove() without await, it returns a cancellable operation object with a result property and cancel() method. This differs from await remove() which directly returns the result but cannot be cancelled.
import { remove } from 'aws-amplify/storage';
let deleteOperation;
// Start deletion when user clicks delete buttonfunction handleDeleteFolder() { // remove() returns { result: Promise, cancel: Function } deleteOperation = remove({ path: 'user-uploads/large-dataset/', options: { onProgress: (fileBatch) => { updateProgressBar(fileBatch.deleted?.length || 0); } } });
// Access the promise via .result property deleteOperation.result.then(result => { console.log('Success', result); }).catch(error => { if (error.name === 'CanceledError') { console.log('Deletion cancelled by user'); } else { console.log('Error:', error); } });}
// Cancel when user clicks cancel or navigates awayfunction handleCancel() { if (deleteOperation) { deleteOperation.cancel(); }}More remove options
| Option | Type | Default | Description |
|---|---|---|---|
| bucket | string | { bucketName: string; region: string; } | Default bucket and region from Amplify configuration | A string representing the target bucket's assigned name in Amplify Backend or an object specifying the bucket name and region from the console. Read more at Configure additional storage buckets |
| expectedBucketOwner | string | Optional | The account ID that owns requested bucket. |
| onProgress | (fileBatch: { deleted?: {path: string}[]; failed?: {path: string; code: string; message: string}[]; }) => void | Optional | Callback function for tracking folder deletion progress. Called after each batch of files is processed during folder deletion operations. |