November 9, 2013 How to delete all files from Amazon S3 bucket

Unfortunately there is no way to remove all files from bucket in Amazon AWS console. But how to remove all files from Amazon S3 bucket? I did that with an s3cmd tool and a small bash script.

First of all you need to install the s3cmd tool. In Ubuntu it is possible with the next command:

sudo apt-get install s3cmd
s3cmd –configure

After that you will be able to remove all files from your bucket with "one line" bash script:

FILES=(`s3cmd ls s3://mybucket | grep -v DIR | awk '{print $4}' | tr '\n' ' '`) && for FILENAME in ${FILES[*]}; do s3cmd del $FILENAME; done

but this will remove only files except directories. To remove directories you need to run another one "one line" script on bash:

DIRS=(`s3cmd ls s3://mybucket | grep DIR | awk '{print $2}' | tr '\n' ' '`) && for DIRNAME in ${DIRS[*]}; do s3cmd del --recursive $DIRNAME; done

That's all. After these commands your bucket should be empty and I hope these commands will help you to save some time

Administration