Create a gist now

Instantly share code, notes, and snippets.

# Deletes documents returned from a view named 'badones' in a design document named 'logging'
# The view must emit doc._id as the key. Here's an example design document
# function (doc) {
# var badword = 'mistake';
# if (doc.anotherprop && doc.anotherprop.substr(0, badword.length) === badword) {
# emit(doc._id, doc.anotherprop);
# }
# }
import requests
import os
from cloudant import cloudant
from cloudant.client import Cloudant
from cloudant.document import Document
CLOUDANT_USERNAME = os.getenv('CLOUDANT_USERNAME', 'inlineusername')
CLOUDANT_PW = os.getenv('CLOUDANT_PW', 'inlinepw')
CLOUDANT_DB = 'thedb'
LIMIT = 200
totalrows = 1
processedrows = 0
if (CLOUDANT_PW):
u = 'https://'+CLOUDANT_USERNAME+':'+CLOUDANT_PW+'@'+CLOUDANT_USERNAME+'.cloudant.com/'
else:
u = 'https://'+CLOUDANT_USERNAME+'.cloudant.com/'
u += CLOUDANT_DB + '/_design/view/_view/vegas?reduce=false&limit='+str(LIMIT)
while (processedrows < totalrows):
r = requests.get(u)
rj = r.json()
if (totalrows==1):
totalrows = rj['total_rows']
processedrows += LIMIT
with cloudant(CLOUDANT_USERNAME, CLOUDANT_PW, account=CLOUDANT_USERNAME) as client:
thedb = client[CLOUDANT_DB]
for doc in rj['rows']:
# print 'Deleting ' + doc['id'] + ': ' + doc['value'] + ' ...'
mydoc = thedb[doc['id']]
mydoc.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment