namalfernandolk namalfernandolk - 2 months ago 31
Java Question

Delete expired documents in CouchDB

I'm using couchDB to store some data and I'm having a problem with deleting the docs that was inserted before n days.

So, I treid to load the document I want do delete through a view and do a DELETE request for all of them at once; but failed.

Sample Document :

{
"date":"20160912",
"_id":"1xxxxx",
"name":"namal"
}


View function :

function (doc) {
if(doc.date){
emit(doc.date,doc);
}
}


URL for loading documents :

http://localhost:5984/dbx/_design/viewx/_view/date?key=%2220160910%22

DELETE request :

curl -X DELETE http://localhost:5984/dbx/_design/viewx/_view/date?key=%2220160910%22


I know that I can do this per document using _id and _rev like below.

curl -X DELETE http://localhost:5984/dbx/1xxxxx?rev="1-1111x1111111x222222x333333"


But in this case, I need to delete resulting document(s) one by one. What I want is, to delete a list of documents (bluk docs) at once (As I mentioned, the actual requremnet is to delete the docs that was inserted before n days).

Further, I know that the better way is to clear the expired data is by setting expiration time using CouchBase or MongoDb. But I'm looking for a solutin within the CouchDB implementation itself.

Note : In this case I want to make them really delete. Not invisible.


  • Language : Java 1.7

  • OS : Ubuntu 14.04 64 bit

  • CouchDB version : 1.6.1


Kul Kul
Answer

You could use CouchDB's bulk document API to update the documents (an update that sets the _deleted field to true is as good as a delete).

More info: http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API#Modify_Multiple_Documents_With_a_Single_Request

For something more permanent, you can use CouchDB's _purge Command. Details here

Note that this operation isn't replicated. So if you have a replication setup, you'll have to do this on each database.