Created
November 15, 2010 16:41
-
-
Save notmyname/700577 to your computer and use it in GitHub Desktop.
quickly delete keys in s3
This file contains 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 eventlet | |
eventlet.monkey_patch() | |
import boto | |
import sys | |
import Queue | |
conn = boto.connect_s3('s3 creds', 'go here') | |
def delete_key(q): | |
count = 0 | |
while True: | |
try: | |
key = q.get_nowait() | |
if key is None: | |
break | |
except Queue.Empty: | |
eventlet.sleep(.1) | |
else: | |
key.delete() | |
count += 1 | |
sys.stdout.write('.') | |
if count % 10 == 0: | |
sys.stdout.flush() | |
results = [] | |
q = Queue.Queue() | |
for bucket in conn.get_all_buckets(): | |
print 'starting delete for bucket %s' % bucket.name | |
for i, key in enumerate(bucket.get_all_keys()): | |
q.put(key) | |
print 'queued %d items' % (i+1) | |
pool = eventlet.GreenPool(size=50) | |
for _ in range(50): | |
results.append(pool.spawn(delete_key, q)) | |
q.put(None) | |
[x.wait() for x in results] | |
for bucket in conn.get_all_buckets(): | |
try: | |
bucket.delete() | |
print 'deleted bucket %s' % bucket.name | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment