Skip to content

Instantly share code, notes, and snippets.

@notmyname
Created November 15, 2010 16:41
Show Gist options
  • Save notmyname/700577 to your computer and use it in GitHub Desktop.
Save notmyname/700577 to your computer and use it in GitHub Desktop.
quickly delete keys in s3
#!/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