Mike Baranczak
Mike Baranczak

Reputation: 8374

How can I delete multiple documents in CouchDB?

I want to delete all documents where foo equals x. Seems like a pretty basic operation, but I just can't figure it out.

I know how to delete an individual document, but that's not good enough - I may have to delete a few thousand at a time.

How do I bulk delete documents in CouchDB?

Upvotes: 26

Views: 20869

Answers (4)

zlr
zlr

Reputation: 829

It's quite easy with bulk delete:
https://docs.couchdb.org/en/stable/api/database/bulk-api.html#updating-documents-in-bulk

Just POST to _all_docs a list of JSONs that look like:

{"_id": "0", "_rev": "1-62657917", "_deleted": true}

Upvotes: 5

galeej
galeej

Reputation: 563

I tried a somewhat long method to delete documents. I first created a view called map_fun that called the documents i wanted to get deleted. I then iterated through the view and stored the keys of allt he documents and used del db['_id'] to delete them

map_fun = function(doc){
    if (doc.doc_type == 'classic'){
    emit(doc._id, doc)
    }}

deldoclist = []
for row in db.query(map_fun):
    deldoclist.append(row.key)

for item in deldoclist:
    del db[item]

Upvotes: 0

Cristiano Santos
Cristiano Santos

Reputation: 2137

I also needed something to handle that and, since there was nothing at the time, I decided to make my own implementation.

You can find it here.

Update

Since it was very helpful to me and in order to protect myself from mistakes, I added a backup/restore feature to this tool that can now be found on version 0.2

Upvotes: 2

lukecampbell
lukecampbell

Reputation: 15256

I don't know if it's the right way but make a view that exposes the foo field, query the view for the doc._ids of all your documents that you want to delete, and make a bulk update against all your documents. So two (ideally) calls to couch.

http://comments.gmane.org/gmane.comp.db.couchdb.user/11222

Has a similar way to go about it.

Do a bulk update on all the documents you want to delete and update doc._deleted=true following the example in Bulk deletion of documents

Upvotes: 11

Related Questions