Skip to content

Instantly share code, notes, and snippets.

@jpetazzo
Created April 4, 2012 23:25
Show Gist options
  • Save jpetazzo/2306534 to your computer and use it in GitHub Desktop.
Save jpetazzo/2306534 to your computer and use it in GitHub Desktop.
Quick and dirty script to wipe out the content of a single Riak bucket
#!/usr/bin/env python
# delete a given riak bucket
import sys
import requests
import json
from pprint import pprint
if len(sys.argv) < 3:
print 'usage: {0} riak_url bucket_name'.format(sys.argv[0])
exit()
riak_url = sys.argv[1]
bucket_name = sys.argv[2]
keys_url = "{0}/riak/{1}?keys=stream".format(riak_url, bucket_name)
print 'fetching keys:', keys_url
result = requests.get(keys_url)
result.raise_for_status()
keys_chunked_json = result.text
def read_next():
global keys_chunked_json
decoder = json.JSONDecoder()
(json_obj, offset) = decoder.raw_decode(keys_chunked_json)
keys_chunked_json = keys_chunked_json[offset:]
return json_obj
print '--- bucket info'
pprint(read_next())
keys_set = set()
while len(keys_chunked_json):
keys_set.update(read_next()['keys'])
print '--- keys'
pprint(keys_set)
yes = raw_input('are you sure you want to delete this bucket? [{0}/riak/bucket/{1}] (write Yes):'.format(riak_url, bucket_name))
assert yes == 'Yes'
for key in keys_set:
url = '{0}/riak/{1}/{2}'.format(riak_url, bucket_name, key)
print 'DELETE', url
requests.delete(url)
print 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment