Delete all files in an S3 bucket

Last year I had sunset a project that was using both AWS’ S3 and Linode’s
S3-compatible object storage offering. After pulling down some final snapshots,
I wanted to delete the buckets on both services.

Similar to the error you receive when you attempt to rmdir a directory with
files in it, you can’t delete an S3 bucket until it is empty. Unfortunately,
especially with S3-compatible implementations, the ability to trash all of the
files may not be readily available from a web UI.

That’s where s3cmd comes in extremely handy. For those not familiar,
s3cmd is a free command-line utility for working with S3 and
S3-compatible services. It also has a handy way to delete all files in a bucket
with ease!

Quite a few companies are offering S3-compatible object storage these days so if
you’re on AWS and looking for an alternative, you may want to check out my VPS
series (updated monthly!).

One more thing, I omitted the --force argument on the commands below to make
things a bit safer for folks that are opting to not read the post and just copy
and paste commands. To be able to remove a bucket and all files, you will
definitely need to include the --force argument.

Delete all files in a bucket

# Just the files
s3cmd del s3://YOURBUCKET

# Files and directories
s3cmd del s3://YOURBUCKET --recursive

Delete all files with a certain prefix in a bucket

# Just the files

# Files and directories
s3cmd del s3://YOURBUCKET/YOURPREFIX --recursive

Delete all files AND the bucket itself

# Just the files

# Files and directories
s3cmd rb s3://YOURBUCKET/YOURPREFIX --recursive
Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

If you found this article helpful, please consider buying me a coffee.