Deleting All Your Files on Amazon S3

Adam Skogman

Deleting all your files on S3 is not as easy as you may think, since it requires you to delete all files, one by one, before you can remove the buckets.

Thankfully, this can be done using a small ruby script. First, install the needed ruby gems:

>sudo gem install right_aws

Then, paste this into your text editor, and edit before you run:

 
#!/usr/bin/env ruby -w
 
require 'rubygems'
require 'right_aws'
 
s3 = RightAws::S3.new(aws_access_key='your-access-key-here', aws_secret_access_key='your-secret-access-key-here')
 
s3.buckets.each do |bucket|
 
  keys = bucket.keys('max-keys'=>1000 )
 
  while keys.length > 0 do
    keys.each do |key|
      puts key.full_name
      key.delete
    end
    keys = bucket.keys('max-keys'=>1000 )
  end
 
  bucket.delete
 
end
 

Note that I only get a 1000 keys at a time. Having a lot of files in a bucket will break most GUI tools, because they try to do to much - like listing all of my files...

Happy deleting!

Tip: Run this for an instance on Amazon, not from your own computer. Running inside Amazon is much faster.

Adam Skogman
Consultant at Jayway

Tags: , ,

2 comments ↓

#1 brandasics on 03.06.11 at 15:59

Where is it? ;-) I need it :-)

#2 William on 03.06.11 at 22:11

This was very helpful — Thanks!

Leave a Comment