Delete all files in an S3 bucket

Josh Sherman
1 min read
Command-line Interface Shell Script

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 Showdown 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
Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

Related Articles