Tue Sep 21 2021·3 min read
Photo by Luke Pamer
To delete an entire folder in an AWS S3 bucket, use the
s3 rm command,
passing in the path of the objects to be deleted along with the
parameter, which applies the action to all files under the specified path.
Let's first run the s3 rm command in test mode to make sure the output matches the expectations:
aws s3 rm s3://YOUR_BUCKET/ --recursive --dryrun --exclude "*" --include "my-folder/*"
The example shows that all of the files in the specified folder would get deleted. The folder also gets deleted because S3 doesn't keep empty folders around.
--includeparameters matters. Filters passed later in the command have higher precedence.
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||we only want to delete the contents of a specific folder, so we exclude all other paths in the bucket|
|include||we include the path that matches all of the files we want to delete|
Now that we've made sure the output from the
s3 rm command is what we
expected, let's run it without the
aws s3 rm s3://YOUR_BUCKET/ --recursive --exclude "*" --include "my-folder/*"
To verify all files in the folder have been successfully deleted, we can run the s3 ls command. If the command receives a path that does not exist, is has no return value.
aws s3 ls s3://YOUR__BUCKET/YOUR_FOLDER --recursive
s3 rmwith the
--dryrunparameter first. Make sure that the command does what you intend, without actually running it.
--exclude parameters can be passed to the
s3 rm command
for more fine grained filtering.
The following command, deletes all objects in the folder, except for objects
aws s3 rm s3://YOUR_BUCKET/ --recursive --exclude "*" --include "my-folder/*" --exclude "*.png"
The output of the
s3 ls command shows that the image at path
my-folder-2/hook-flow.png has not been deleted.
If you wanted to preserve all
.png files and all
.txt files you would just
--exclude "*.txt" flag at the end of the command.
--includeparameters is very important. For instance, if we reverse the order and pass
--include "my-folder-2/*"before the
exclude "*"parameter, that would delete all of the files in the S3 bucket because
includeand overrides it.
Finally, let's look at an example where we have the following folder structure:
bucket my-folder-3/ image.webp file.json nested-folder/ file.txt file.json
We have a nested folder that we want to preserve, but we want to delete all of
the files in the
aws s3 rm s3://YOUR_BUCKET/ --recursive --exclude "*" --include "my-folder-3/*" --exclude "my-folder-3/nested-folder/*"
To verify the nested folder did not get deleted, we can run the
s3 ls command:
aws s3 ls s3://YOUR_BUCKET/my-folder-3/nested-folder --recursive --human-readable
The output shows that the nested folder was excluded successfully and has not been deleted.