Archiv der Kategorie: Database

Bulk delete in CouchDB

What: Deleting all documents from Couchdb with a single command from the command line without deleting the database/design documents
Why: Truncate the database
How: Python3 and requests

Retrieve all documents

Couchdb has a rest api, which allows the retrieval of all documents from a database. To delete documents, the corresponding id and revision of each document is needed. Further attributes of the document can be ignored.

To retrieve all documents, a simple get request is enough, which will return a json document with an attribute rows which contains a list of ids and revisions of all documents:

import json
import requests


Set delete flag

Documents can be deleted from Couchdb by setting the attribute _deleted to true (for some subtleties see: this blog). Lets create the minimal information for deletion for each document:

for doc in rows:
    todelete.append({"_deleted": True, "_id": doc["id"], "_rev": doc["value"]["rev"]})

Push changes

While all documents can be retrieved from the data base at once, it is also possible to submit multiple documents in one request:"http://localhost:5984/databasename/_bulk_docs", json={"docs": todelete})

Make it user friendly

To make it a little bit more user friendly, the name of the database can be set as argument and the python script should be called from the command line. In the end it looks like:

#!/usr/bin/env python3
# coding: utf-8
import json
import requests
import sys

if len(database)==0:


for doc in rows:
    todelete.append({"_deleted": True, "_id": doc["id"], "_rev": doc["value"]["rev"]})"http://localhost:5984/{}/_bulk_docs".format(database), json={"docs": todelete})

Have fun in extending the script and use it for maintaining your Couchdb!