DanL
DanL

Reputation: 111

Can I retrieve all revisions of a deleted document?

I know I can retrieve all revisions of an "available" document, but can I retrieve the last "available" version of a deleted document? I do not know the revision id prior to the delete. This is the command I am currently running...it returns {"error":"not_found","reason":"deleted"}.

curl -X GET http://localhost:5984/test_database/a213ccad?revs_info=true

Upvotes: 11

Views: 8291

Answers (4)

nlawson
nlawson

Reputation: 11620

Besides _changes, another good way to do this is to use keys with _all_docs:

GET $MYDB/_all_docs?keys=["foo"] ->

{
    "offset": 0,
    "rows": [
        {
            "id": "foo",
            "key": "foo",
            "value": {
                "deleted": true,
                "rev": "2-eec205a9d413992850a6e32678485900"
            }
        }
    ],
    "total_rows": 0
}

Note that it has to be keys; key will not work, because only keys returns info for deleted docs.

Upvotes: 2

avalez
avalez

Reputation: 1217

I've got this problem, trying to recover deleted document, here is my solution:

0) until you run a compaction, get deleted history, e.g.:

curl http://example.iriscouch.com/test/_changes

1) you'll see deleted documents with $id and $rev, put empty document as new version, e.g.:

curl -X PUT http://example.iriscouch.com/test/$id?rev=$rev -H "Content-Type: application/json" -d {}

2) now you can get all revisions info, e.g:

curl http://example.iriscouch.com/test/$id?revs_info=true

See also Retrieve just deleted document

Upvotes: 6

natevw
natevw

Reputation: 17902

You can get the last revision of a deleted document, however first you must first determine its revision id. To do that, you can query the _changes feed and scan for the document's deletion record — this will contain the last revision and you can then fetch it using docid?rev=N-XXXXX.

I remember some mailinglist discussion of making this easier (as doing a full scan of the changes feed is obviously not ideal for routine usage), but I'm not sure anything came of it.

Upvotes: 1

Jim
Jim

Reputation: 116

I've hit this several times recently, so for anyone else wandering by ...

This question typically results from a programming model that needs to know which document was deleted. Since user keys such as 'type' don't survive deletion and _id is best assigned by couch, it would often be nice to peak under the covers and see something about the doc that was deleted. An alternative is to have a process that sets deleted:True (no underscore) for documents, and to adjust any listener filters, etc., to look for deleted:True. One of the processes can then actually delete the document. This means that any process triggering on the document doesn't need to track an _id for eventual deletion.

Upvotes: 0

Related Questions