DEV Community

Cover image for Deleting all S3 buckets in a single CLI command
JavaScript Room
JavaScript Room

Posted on

Deleting all S3 buckets in a single CLI command

Hi everyone!
This is more like a cheatsheet for myself, but maybe it will be also helpful for someone else. If you use AWS S3 storage service you probably have noticed that you cannot remove not empty buckets straight away via website, you have to clean up the content first. And then, when you have a bunch of buckets you want to delete, it's a bit of pain to remove them one by one via the AWS website interface. AWS command line interface can save us some time here. Don't forget to install it and configure your credentials. Then you can use the commands below in your terminal.

Delete all S3 buckets stored in your AWS account (including not empty):

aws s3 ls | cut -d" " -f 3 | xargs -I{} aws s3 rb s3://{} --force
Enter fullscreen mode Exit fullscreen mode

Delete only empty buckets (without --force flag):

aws s3 ls | cut -d" " -f 3 | xargs -I{} aws s3 rb s3://{}
Enter fullscreen mode Exit fullscreen mode

Delete only buckets with the prefix old_:

aws s3 ls | grep 'old_*' | cut -d" " -f 3 | xargs -I{} aws s3 rb s3://{} --force
Enter fullscreen mode Exit fullscreen mode

If you have any questions or suggestion on the commands don't hesitate to leave a comment below. Have a productive day!

Top comments (2)

Collapse
 
memattchung profile image
memattchung

Nice tip! Of course, if you are running a production service: exercise a ton of caution when executing this command. In fact, tap a colleague on the shoulder and have them verify what you are going to do because this command has massive blast radius.

Cheers,
@memattchung

Collapse
 
room_js profile image
JavaScript Room

Absolutely! Confirming it with all colleagues upfront is important step :) This is a very dangerous command.