CouchDB – Delete Document

To delete a document from CouchDB database, you can either use CouchDB Web Interface or send a DELETE request to the REST API of CouchDB.

Delete Document via REST API

Send a HTTP DELETE request with the following URL.

http://hostname/database_name/document_id/?rev=revision_number

We have our CouchDB running in our localhost. Hence, we shall use 127.0.0.1:5984 as hostname.

We will use an existing database named tutorialkart.

Let us delete document with id 0005.

You can get the _rev filed value, by sending a GET request to get the document details. For details, refer CouchDB – GET Document. Following is the response for the GET request for document with id 0005.

 {
     "_id": "0005",
     "_rev": "4-41144c540298e1340e312de91319d6f3",
     "tutorial": "PostgreSQL Tutorial",
     "category": "RDBMS",
     "topics": 22
 }

So, from the above response, version_number would be 4-41144c540298e1340e312de91319d6f3. revision_number is sent as parameter in the DELETE request.

The resulting request URL that we have to use for DELETE request will become,

http://127.0.0.1:5984/tutorialkart/0005/?rev=4-41144c540298e1340e312de91319d6f3

We will use Postman, to trigger a PUT request with the URL to update a document in CouchDB Database. You can use any other CLI or GUI tool of your choice.

CouchDB - Delete Document - REST API

Let us check if the document is deleted.

Send a HTTP GET Request with the same URL without rev param.

CouchDB - Delete Document - REST API - Confirmation

The document has been deleted. In the response, we got error as not_found and reason as deleted. Since, the document is deleted.

Delete Document via CouchDB Web Interface

You can also delete a document in CouchDB database using Web Interface.

Open URL http://hostname/_utils/ in your browser. In this example, the URL will be http://127.0.0.1:5984/_utils/.

Click on Databases tab present in the left panel. Click on the database in which you would like to delete the document. To view the document, open Table view and click on the document.

CouchDB Delete Document - CouchDB Web Interface

When you click on the document, an editor will be opened with the JSON document. Click on the Delete button present in the right side, highlighted in the following screenshot.

CouchDB - Delete Document - Click on Delete button

You will get an alert to confirm the deletion. Click on Delete Document.

CouchDB - Delete Document - Confirm Deletion

Once you click on Delete Document button, you would see a message “Your document has been successfully deleted.”

CouchDB - Delete Document - Successfully Deleted

Summary

In this CouchDB Tutorial, we learned to delete a document in CouchDB database via HTTP REST API and CouchDB Web Interface.