Wednesday, 9 November 2011

s3cmd : Delete all files in a bucket


Unfortunately, s3cmd cannot delete files with a wildcard.  You cannot just

s3cmd del s3://my-bucket/*

Well, you can, s3cmd will also report "Object * deleted" but all your files stay in the bucket!
I know why guys in Amazon decided to do so but that is a long story, let's go back to practice.
I suggest a unix-way here (well, I would suggest it anyway):

s3cmd ls s3://my-bucket/  | awk '{print $4}' | xargs s3cmd del

That is all, You will get list of files in an output.

Another way, is to "s3cmd sync" your bucket against an empty directory

4 comments:

  1. Like you said, I think it's only as simple as this:

    s3cmd sync --recursive --delete-removed directory/ s3://mybucket-name/

    ReplyDelete
    Replies
    1. This works well. Just sync with an empty directory and you're good to go.

      Delete
  2. s3cmd del s3://bucketname/ --recursive

    ReplyDelete
  3. arcseldon's solution worked perfectly for me, thanks!

    ReplyDelete