26

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?

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71

4 Answers4

11

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

okev
  • 9
  • 1
  • 4
lukecampbell
  • 14,728
  • 4
  • 34
  • 32
  • 5
    OK, so based on your link, and a few other articles I've read in the meantime, it seems that doing a single-step bulk delete (or update) is impossible. It seems pretty shocking that something which calls itself a "database" wouldn't have this feature, but at least now I know the least-inefficient way to get what I want. Thanks for the information. – Mike Baranczak May 01 '12 at 22:40
  • 11
    It's true. The definition of "database" is changing. People used to call memcache a "cache" and now it is considered an "in-memory database." I agree that it's disconcerting. But on the other hand, the old definition really meant "SQL database" which was not correct either. – JasonSmith May 02 '12 at 02:41
  • really sad there isn't a more convenient way... :'( – Hendy Irawan Nov 30 '14 at 15:18
  • 4
    @MikeBaranczak It wouldn't seem shocking to you if you had read the conceptual basics of CouchDB, one of which is that consistency is only guaranteed per document (there are no transactions). Agreed, this is a restriction, but enables CouchDB to be the powerful yet simple database it is. And if you know the concepts, you will be able to design your application in a way that the lack of transactions is not an issue. – theDmi Jan 03 '15 at 14:29
3

It's quite easy with bulk delete: https://wiki.apache.org/couchdb/HTTP_Bulk_Document_API Just POST to _all_docs a list of JSONs that look like:

{"_id": "0", "_rev": "1-62657917", "_deleted": true}
zlr
  • 789
  • 11
  • 22
  • No, that doesn't answer the question. I wanted the equivalent of something like `DELETE FROM t WHERE foo=10`. Using the bulk operations, I'd still have to make two calls, not one. – Mike Baranczak Apr 03 '17 at 20:27
  • 1
    Yes of course you would. In the same way that there's no equivalent of `UPDATE ... WHERE ...` there's no equivalent of `DELETE ... WHERE ...` – smathy Aug 09 '19 at 22:56
  • 2
    The link is dead. this is now in the docs here:https://docs.couchdb.org/en/stable/api/database/bulk-api.html#updating-documents-in-bulk – Bob Gear Nov 10 '19 at 11:01
2

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

Community
  • 1
  • 1
Cristiano Santos
  • 2,157
  • 2
  • 35
  • 53
0

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]
Marco
  • 1,073
  • 9
  • 22
galeej
  • 535
  • 9
  • 23