To start off, here are the links to my previous posts about CouchDB:
- Relaxing on the Couch(DB)
- Installing the Couch(DB)
- PUTting the Couch(DB) in Your Living Room
- GETting Documents From CouchDB
Today, I want to talk about how to delete a document from CouchDB. In order to do that, we have to use the HTTP DELETE operation (how convenient). Removing a document from CouchDB can be done using the following request:
DELETE /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb ?rev=1-2901013762
This makes CouchDB return the following response:
{ "ok":true, "id":"13ce4780-62c8-4074-9955-8c99966b84bb", "rev":"2-3500205450" }
This confirms that the document has been removed by CouchDB. But is it really gone? Not exactly. We might be able to retrieve it using its revision number. Sending the following request:
GET /documentstore/13ce4780-62c8-4074-9955-8c99966b84bb ?rev=2-3500205450
still indicates that the document we just deleted still exists:
{ "_id":"13ce4780-62c8-4074-9955-8c99966b84bb", "_rev":"2-3500205450", "_deleted":true }
Notice that the _deleted attribute indicates that the document has indeed been deleted. It’s even possible to resurrect the document at hand by performing an update, bringing the document back amongst the living documents.
However, I don’t consider that a good idea in all scenarios. In fact, as pointed out by this comment on my previous post, one should not blindly rely on the MVCC tokens for version control of documents, something that I neglected to point out in that post. When CouchDB is configured for compaction or replication with other instances of CouchDB, then this approach is not recommended. In case of compaction , CouchDB will actively purge old versions of documents and deleted documents. In case of replication, a particular node in a clustered environment doesn’t necessarily have the complete version history of a document. Bottom line, use this feature very carefully.
For my next post, I will talk about how to create attachment for a document.
Till next time
A good friend is like a computer;me’enter’ur life,’save’u in my heart,’format ur problems,’shift’u 2opportunities&never’delete’u from memory