Skip to content

Instantly share code, notes, and snippets.

@chuenniger
Created September 16, 2021 10:37
Show Gist options
  • Save chuenniger/7beb2e00a9ed9c176a554dad7879e927 to your computer and use it in GitHub Desktop.
Save chuenniger/7beb2e00a9ed9c176a554dad7879e927 to your computer and use it in GitHub Desktop.
CouchDB - Delete documents by view
import couchdb
user = ''
password = ''
db_name = ''
view = 'debug/all_events'
conn_string = couchdb.Server(
'http://%s:%s@localhost:5984/' % (user, password)
)
db = conn_string[db_name]
amount_objects_to_delete = 20000
print("Start delete and compact routine")
count = 0
log_steps = 100
while True:
print("... Requesting next %d documents...", amount_objects_to_delete)
items = db.view(
view,
limit=amount_objects_to_delete,
include_docs=True
)
if len(items) == 0:
print("No documents available")
break
for item in items:
count += 1
db.delete(item['doc'])
if count % log_steps == 0:
print("%s", count)
print("Deleted %d documents, requesting compact" % count)
# prune all deleted documents
db.compact()
print("Finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment