Skip to content

Instantly share code, notes, and snippets.

@OnlyInAmerica
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OnlyInAmerica/d11f52b73b5e02c858ce to your computer and use it in GitHub Desktop.
Save OnlyInAmerica/d11f52b73b5e02c858ce to your computer and use it in GitHub Desktop.
Delete old S3 Buckets
'''
Find oldest created buckets in your AWS account and optionally delete them
Requirements:
Environmental Variables:
+ AWS_ACCESS_KEY_ID
+ AWS_SECRET_ACCESS_KEY
Python packages:
+ boto
'''
MAX_BUCKETS_TO_DELETE = 4
# These bucket names will never be selected for deletion.
DO_NOT_DELETE = [
'dbro',
'openwatch-audio',
'openwatch-avatar',
'openwatch-backups',
'openwatch-capture',
'openwatch-capture-rejected',
'openwatch-hls',
'openwatch-livestreamer',
'openwatch-log',
'openwatch-media',
'openwatch-photo',
'openwatch-static',
'openwatch-storage',
'kickflip-static',
'kickflip-billing'
]
from operator import itemgetter
import boto
def delete_recursive(bucketname, keyprefix):
'''
Recusively delete all keys with given prefix from the named bucket
Stolen from http://stackoverflow.com/a/10055320/141084
'''
s3 = boto.connect_s3()
bucket = s3.get_bucket(bucketname, validate=False)
bucketListResultSet = bucket.list(prefix=keyprefix)
return bucket.delete_keys([key.name for key in bucketListResultSet])
con = boto.connect_s3()
buckets = con.get_all_buckets()
bucket_date = [{'date': bucket.creation_date , 'bucket':bucket} for bucket in buckets]
sorted_bucket_date = sorted(bucket_date, key=itemgetter('date'))
print str(len(buckets)) + " Total buckets"
print 'The following bucket should be deleted next'
print 'Creation Date \t\t\t Bucket'
print '----------------------------------------'
to_delete = []
for bucket in sorted_bucket_date:
if bucket['bucket'].name not in DO_NOT_DELETE:
to_delete.append(bucket)
if len(to_delete) >= MAX_BUCKETS_TO_DELETE:
break
for bucket in to_delete:
print bucket['date'] + '\t' + bucket['bucket'].name
if len(to_delete) == 0:
print 'No suitable buckets to delete'
else:
# DANGER ZONE
#input("Press any key to delete buckets...")
#for bucket in to_delete:
# delete_recursive(to_delete['bucket'].name, '*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment