English 中文(简体)
CouchDB - Deleting a Document
  • 时间:2024-12-22

CouchDB - Deleting a Document


Previous Page Next Page  

Deleting a Document using cURL Utipty

You can delete a document in CouchDB by sending an HTTP request to the server using DELETE method through cURL utipty. Following is the syntax to delete a document.

curl -X DELETE http : // 127.0.0.1:5984 / database name/database id?_rev id

Using −X, we can specify a custom request method of HTTP we are using, while communicating with the HTTP server. In this case, we are using Delete method. To delete a database /database_name/database_id/ is not enough. You have to pass the recent revision id through the url. To mention attributes of any data structure "?" is used.

Example

Suppose there is a document in database named my_database with document id 001. To delete this document, you have to get the rev id of the document. Get the document data as shown below.

$ curl -X GET http://127.0.0.1:5984/my_database/001
{
   " _id " : " 001 ",
   " _rev " : " 2-04d8eac1680d237ca25b68b36b8899d3 " ,
   " age " : " 23 "
}

Now specify the revision id of the document to be deleted, id of the document, and database name the document belongs to, as shown below −

$ curl -X DELETE http://127.0.0.1:5984/my_database/001?rev=1-
3fcc78daac7a90803f0a5e383f4f1e1e

{"ok":true,"id":"001","rev":"2-3a561d56de1ce3305d693bd15630bf96"}

Verification

To verify whether the document is deleted, try to fetch the document by using the GET method. Since you are fetching a deleted document, this will give you an error message as shown below −

$ curl -X GET http://127.0.0.1:5984/my_database/001
{"error":"not_found","reason":"deleted"}

Deleting a Document using Futon

First of all, verify the documents in the database. Following is the snapshot of the database named tutorials_point.

Deleting Document

Here you can observe, the database consists of three documents. To delete any of the documents say 003, do the following −

    Cpck on the document, you will get a page showing the contents of selected document in the form of field-value pairs.

    This page also contains four options namely Save Document, Add Field, Upload Attachment, Delete Document.

    Cpck on Delete Document option.

    You will get a dialog box saying "Are you sure you want to delete this document?" Cpck on delete, to delete the document.

Delete Document2 Advertisements