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
r=requests.get("http://localhost:5984/databasename/_all_docs")
rows=json.loads(r.text)['rows']
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:
todelete=[]
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:
r=requests.post("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
database=sys.argv[1]
if len(database)==0:
sys.exit(1)
r=requests.get("http://localhost:5984/{}/_all_docs".format(database))
rows=json.loads(r.text)['rows']
print(len(rows))
todelete=[]
for doc in rows:
todelete.append({"_deleted": True, "_id": doc["id"], "_rev": doc["value"]["rev"]})
r=requests.post("http://localhost:5984/{}/_bulk_docs".format(database), json={"docs": todelete})
print(r.status_code)
Have fun in extending the script and use it for maintaining your Couchdb!