Jim Isaacs
Jim Isaacs

Reputation: 103

How do you delete a couchdb document with an empty "" document id?

I see the document in the db as this: {_id: "", _rev: "1-2f11e026763c10730d8b19ba5dce7565", forbidden: "must supply latest _rev to update existing package"}

Everything I see in the docs shows referring to the document with the ID, but of course this can't happen.

I'm not exactly sure how this happened, most likely something faulty between the chair and keyboard, but I wanted to know if there was a possibility of fixing this without starting over.

Upvotes: 4

Views: 1026

Answers (2)

Aaron Arthurs
Aaron Arthurs

Reputation: 111

Concerning version 2.1.1:

There is a bug in Project Fauxton that allows you to clone a document while leaving the new ID blank. Neither the Project Fauxton nor CouchDB's API offers a way to edit or delete documents with blank IDs.

In JasonSmith's answer, the del_blank update function throws {"error":"illegal_docid","reason":"Document id must not be empty"}. So, you must replicate the database into a new database, which will store documents with non-empty IDs.

Upvotes: 0

JasonSmith
JasonSmith

Reputation: 73752

There is a bug in CouchDB that allows this to happen. I believe it was introduced in v1.1.0 and will be fixed in v1.1.1 and v1.2.0.

The bug is that _update functions can create documents with empty-string IDs. To delete the document, use the same update function and exploit the same bug.

For example:

{ "_id": "_design/example",
  "updates": {
    "del_blank":
    "function(doc, req) {
       var doc = {_id:'', _rev:req.query.rev, _deleted:true};
       return [doc, 'Trying to delete nastydoc@'+doc._rev];
     }"
   }
}

Simply provide the revision that is giving you problems and it will mark it deleted.

$ rev="1-2f11e026763c10730d8b19ba5dce7565"
$ curl -XPOST localhost:5984/db/_design/example/_update/del_blank?rev=$rev
Trying to delete nastydoc@1-2f11e026763c10730d8b19ba5dce7565

$ curl localhost:5984/db/_all_docs
{"total_rows":1,"offset":0,"rows":[
{"id":"_design/example","key":"_design/test","value":{"rev":"2-b9bfbedff0c09fab88ff36d06cec0d34"}},
]}

Upvotes: 7

Related Questions