Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Amazon S3 buckets? [closed]

I've been interacting with Amazon S3 through S3Fox and I can't seem to delete my buckets. I select a bucket, hit delete, confirm the delete in a popup, and... nothing happens. Is there another tool that I should use?

like image 845
Kyle Cronin Avatar asked Aug 26 '08 02:08

Kyle Cronin


People also ask

Why can't I delete my S3 bucket?

Short description. You can't delete an S3 bucket using the Amazon S3 console if the bucket contains 100,000 or more objects. You can't delete an S3 bucket using the AWS CLI if versioning is enabled. For more information, see Deleting or emptying a bucket.

How long until S3 bucket is deleted?

When you delete a bucket, there may be a delay of up to one hour before the bucket name is available for reuse in a new region or by a new bucket owner. If you re-create the bucket in the same region or with the same bucket owner, there is no delay.

What happens to an object when we delete it from Amazon S3?

If the version ID maps to a specific object version, Amazon S3 deletes the specific version of the object. If the version ID maps to the delete marker of that object, Amazon S3 deletes the delete marker. This makes the object reappear in your bucket.

Will I be charged for an empty S3 bucket?

Empty S3 buckets don't cost anything. You are only charged by the size of the objects in the bucket, the storage class and the access.


2 Answers

It is finally possible to delete all the files in one go using the new Lifecycle (expiration) rules feature. You can even do it from the AWS console.

Simply right click on the bucket name in AWS console, select "Properties" and then in the row of tabs at the bottom of the page select "lifecycle" and "add rule". Create a lifecycle rule with the "Prefix" field set blank (blank means all files in the bucket, or you could set it to "a" to delete all files whose names begin with "a"). Set the "Days" field to "1". That's it. Done. Assuming the files are more than one day old they should all get deleted, then you can delete the bucket.

I only just tried this for the first time so I'm still waiting to see how quickly the files get deleted (it wasn't instant but presumably should happen within 24 hours) and whether I get billed for one delete command or 50 million delete commands... fingers crossed!

like image 146
chris14679 Avatar answered Oct 08 '22 04:10

chris14679


Remeber that S3 Buckets need to be empty before they can be deleted. The good news is that most 3rd party tools automate this process. If you are running into problems with S3Fox, I recommend trying S3FM for GUI or S3Sync for command line. Amazon has a great article describing how to use S3Sync. After setting up your variables, the key command is

./s3cmd.rb deleteall <your bucket name> 

Deleting buckets with lots of individual files tends to crash a lot of S3 tools because they try to display a list of all files in the directory. You need to find a way to delete in batches. The best GUI tool I've found for this purpose is Bucket Explorer. It deletes files in a S3 bucket in 1000 file chunks and does not crash when trying to open large buckets like s3Fox and S3FM.

I've also found a few scripts that you can use for this purpose. I haven't tried these scripts yet but they look pretty straightforward.

RUBY

require 'aws/s3'  AWS::S3::Base.establish_connection!( :access_key_id => 'your access key', :secret_access_key => 'your secret key' )  bucket = AWS::S3::Bucket.find('the bucket name')  while(!bucket.empty?) begin puts "Deleting objects in bucket"  bucket.objects.each do |object| object.delete puts "There are #{bucket.objects.size} objects left in the bucket" end  puts "Done deleting objects"  rescue SocketError puts "Had socket error" end  end 

PERL

#!/usr/bin/perl use Net::Amazon::S3; my $aws_access_key_id = 'your access key'; my $aws_secret_access_key = 'your secret access key'; my $increment = 50; # 50 at a time my $bucket_name = 'bucket_name';  my $s3 = Net::Amazon::S3->new({aws_access_key_id => $aws_access_key_id, aws_secret_access_key => $aws_secret_access_key, retry => 1, }); my $bucket = $s3->bucket($bucket_name);  print "Incrementally deleting the contents of $bucket_name\n";  my $deleted = 1; my $total_deleted = 0; while ($deleted > 0) { print "Loading up to $increment keys...\n"; $response = $bucket->list({'max-keys' => $increment, }) or die $s3->err . ": " . $s3->errstr . "\n"; $deleted = scalar(@{ $response->{keys} }) ; $total_deleted += $deleted; print "Deleting $deleted keys($total_deleted total)...\n"; foreach my $key ( @{ $response->{keys} } ) { my $key_name = $key->{key}; $bucket->delete_key($key->{key}) or die $s3->err . ": " . $s3->errstr . "\n"; } } print "Deleting bucket...\n"; $bucket->delete_bucket or die $s3->err . ": " . $s3->errstr; print "Done.\n"; 

SOURCE: Tarkblog

Hope this helps!

like image 41
MattLor Avatar answered Oct 08 '22 04:10

MattLor