11

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DanL
  • 111
  • 1
  • 3
  • Has a database compaction been performed after the delete? – bdargan Apr 01 '11 at 10:03
  • 2
    Couch's versioning is there to serve its needs, not ours. Old versions aren't necessarily replicated, and old versions can go away during compaction. – Wayne Conrad May 13 '11 at 18:33
  • 1
    This is not true, the last deleted version of a document is always kept to ensure consistency for replication. Only older revisions are removed on compaction. – smathy Jun 26 '12 at 03:16

4 Answers4

6

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

Community
  • 1
  • 1
avalez
  • 1,217
  • 1
  • 10
  • 15
  • 1
    You will still see deleted documents in _changes even after a compaction. A compaction just removes non-leaf document bodies, all [well, up to revs_limit] revision metadata is preserved as well as the final deleted version(s) of a document even after compacting. – natevw Feb 13 '13 at 20:45
2

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.

nlawson
  • 11,510
  • 4
  • 40
  • 50
1

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.

natevw
  • 16,807
  • 8
  • 66
  • 90
0

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.

Jim
  • 116
  • 3
  • You can accomplish something like this by saving the doc with _deleted:true (and any other fields you'd like to store alongside) and having the dependent processes in such a programming model just monitor _changes to do any cleanup they like in response to deletions. – natevw May 02 '12 at 23:02