How to delete multiple files in S3 bucket with AWS CLI
s3 rm
cannot delete multiple files, but you can use s3api delete-objects
to achieve what you want here.
Example
aws s3api delete-objects --bucket x.y.z --delete '{"Objects":[{"Key":"worksheet.xlsx"},{"Key":"purple.gif"}]}'
You can do this by providing an --exclude
or --include
argument multiple times. But, you'll have to use --recursive
for this to work.
When there are multiple filters, remember that the order of the filter parameters is important. The rule is the filters that appear later in the command take precedence over filters that appear earlier in the command.
aws s3 rm s3://x.y.z/ --recursive --exclude "*" --include "purple.gif" --include "worksheet.xlsx"
Here, all files will be excluded from the command except for purple.gif and worksheet.xlsx.
If you're unsure, always try a --dryrun
first and inspect which files will be deleted.
Source: Use of Exclude and Include Filters