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.
You can also perform a remove
operation to a specific bucket by providing the bucket
option. You can pass in a StorageBucket
object representing the target bucket from the name defined in the Amplify Backend.
final result = await Amplify.Storage.remove( path: const StoragePath.fromString('path/to/file.txt'), options: StorageRemoveOptions( // Specify a target bucket using name assigned in Amplify Backend bucket: StorageBucket.fromOutputs('secondBucket'), ),).result;
Alternatively, you can also pass in an object by specifying the bucket name and region from the console.
final result = await Amplify.Storage.remove( path: const StoragePath.fromString('path/to/file.txt'), options: StorageRemoveOption( // Alternatively, provide bucket name from console and associated region bucket: StorageBucket.fromBucketInfo( BucketInfo( bucketName: 'second-bucket-name-from-console', region: 'us-east-2', ), ), ),).result;
Remove multiple Files
You can remove multiple files using Amplify.Storage.removeMany
, as well as specify a bucket to target, the files to be removed in a batch should have the same access level:
Future<void> remove() async { try { final result = await Amplify.Storage.removeMany( paths: [ const StoragePath.fromString('public/file-1.txt'), const StoragePath.fromString('public/file-2.txt'), ], // if this option is not provided, the default bucket in the Amplify Backend will be used options: StorageRemoveManyOptions( bucket: StorageBucket.fromOutputs('secondBucket'), /* Alternatively, provide bucket name from console and associated region bucket: StorageBucket.fromBucketInfo( BucketInfo( bucketName: 'second-bucket-name-from-console', region: 'us-east-2', ), ), */ ), ).result; safePrint('Removed files: ${result.removedItems}'); } on StorageException catch (e) { safePrint(e.message); }}