Last active
October 9, 2015 10:28
-
-
Save jerem/3489032 to your computer and use it in GitHub Desktop.
A python/gevent script to quickly delete a very large Amazon S3 bucket.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import gevent.monkey | |
gevent.monkey.patch_all() | |
import sys | |
import optparse | |
import gevent | |
from boto.s3.connection import S3Connection | |
from boto.s3.bucket import Bucket | |
def delete(access_key, access_secret, bucket_name, concurency): | |
conn = S3Connection(access_key, access_secret) | |
bucket = Bucket(conn, bucket_name) | |
i = 0 | |
while True: | |
keys = bucket.get_all_keys() | |
if not keys: | |
break | |
subset = [] | |
for k in keys: | |
subset.append(k) | |
i += 1 | |
if len(subset) == concurency: | |
print(('Deleting {0} keys... (total = {1})'.format(len(subset), i))) | |
threads = [gevent.spawn(bucket.delete_key, s) for s in subset] | |
gevent.joinall(threads) | |
subset = [] | |
conn.delete_bucket(bucket) | |
print('Bucket deleted!') | |
def main(argv): | |
parser = optparse.OptionParser() | |
parser.set_usage('%prog [options] <bucket>') | |
parser.add_option( | |
'-a', '--access_key', | |
dest='access_key', type='string', | |
help='Your AWS Access Key ID' | |
) | |
parser.add_option( | |
'-s', '--access_secret', | |
dest='access_secret', type='string', | |
help='Your AWS Secret Access Key' | |
) | |
parser.add_option( | |
'-c', '--concurrency', | |
dest='concurrency', type='int', | |
help='Number of keys to delete simultaneously', | |
default=20 | |
) | |
options, args = parser.parse_args() | |
if not args or len(args) != 1: | |
parser.print_help() | |
sys.exit(1) | |
delete( | |
options.access_key, | |
options.access_secret, | |
args[0], | |
options.concurrency | |
) | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment