Skip to content

Instantly share code, notes, and snippets.

@rodolfo42
Last active December 17, 2015 14:29
Show Gist options
  • Save rodolfo42/5625123 to your computer and use it in GitHub Desktop.
Save rodolfo42/5625123 to your computer and use it in GitHub Desktop.
Deletes all objects in a S3 bucket, so you can finally delete it
# Deletes all objects in a S3 bucket, so you can finally delete it
# inspired by
# http://stackoverflow.com/a/1179190
require 'aws'
require 'yaml'
credentials = {
:access_key_id => 'YOUR_ACCESS_KEY_ID',
:secret_access_key => 'YOUR_SECRET_ACCESS_KEY'
}
bucket_name = 'YOUR_BUCKET_NAME'
AWS.config(credentials)
s3 = AWS::S3.new
bucket = s3.buckets.find(bucket_name).first
while(!bucket.empty?)
begin
puts "Deleting objects in bucket #{bucket_name}"
bucket.objects.each do |object|
print "> Deleting object #{object.key} ..."
object.delete
puts "done"
end
puts "Done deleting objects"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment