Last updated: Sep 21, 2021
Check out my new book
To delete multiple files from an S3 Bucket with the AWS CLI, run the
command, passing in the
include parameters to filter the files
the command is applied to.
Let's run the command in test mode first. By setting the
we instruct the AWS CLI to only print the outputs of the
command, without actually running it.
aws s3 rm s3://YOUR_BUCKET/YOUR_FOLDER/ --dryrun --recursive --exclude "*" --include "file1.txt" --include "file2.txt"
The output shows the names of the files that would get deleted, had we run the
command without the
We've passed the following parameters to the
s3 rm command:
|recursive||applies the |
|dryrun||shows the command's output without actually running it|
|exclude||a filter specifying which paths should be excluded from the command|
|include||a filter specifying which paths should be included in the command|
--includeparameters matters. Filters passed later in the command have higher precedence and override those that come before them.
This means that passing the
--exclude "*" parameter after
--include "file1.txt" would delete all files in the S3 bucket.
s3 rmwith the
--dryrunparameter first. Make sure that the command does what you intend, without actually running it.
In the example above, both of the files are located in the same folder,
otherwise we would include the path to the files in the
aws s3 rm s3://YOUR_BUCKET/ --dryrun --recursive --exclude "*" --include "folder1/file1.txt" --include "folder2/file2.txt"
s3 rmoperation, simply remove the
For the commands that follow, I'll remove the
include parameter can also be set as a matcher, for example to delete all
files with a
.png extension, under a specific prefix, use the following
aws s3 rm s3://YOUR_BUCKET/ --recursive --exclude "*" --include "folder2/*.png"
--includeparameter matches files with the
.pngextension in nested directories.
The command deleted
my-folder/image.png as well as
s3 rm command with an
--include parameter that does not match
any files, produces no output.