couch-db
  1. couch-db-delete-document

Delete Document - (CouchDB Document)

CouchDB is a popular NoSQL database that stores data in the form of documents. Deleting a document in CouchDB removes it permanently from the database. In this tutorial, we'll discuss the syntax, example, output, explanation, use, important points, and summary of deleting a document in CouchDB.

Syntax

To delete a document in CouchDB, you need to make an HTTP DELETE request to the URL of the document you want to delete. The URL is in the form:

http://localhost:5984/{database}/{document-id}?rev={document-revision}
  • database: The name of the CouchDB database that contains the document.
  • document-id: The unique identifier of the document.
  • document-revision: The current revision number of the document.

Example

To delete a document in CouchDB, you can use the following command in the terminal:

curl -X DELETE http://localhost:5984/mydatabase/mydocument?rev=1-abc123

This command will make an HTTP DELETE request to the document URL with the given revision number. If successful, the document will be permanently deleted from the database.

Output

The output of the curl command will be the HTTP response from CouchDB. If the document was deleted successfully, the response will contain a status code of 200 OK and a JSON object with the message "ok".

{"ok":true,"id":"mydocument","rev":"2-def456"}

Explanation

In this example, we used the curl command to make an HTTP DELETE request to the URL of the document we want to delete. The document ID is mydocument and the revision number is 1-abc123.

When CouchDB receives the request, it checks that the given revision number of the document matches the current revision number in the database. If they match, the document is permanently deleted from the database.

Use

Deleting a document in CouchDB is useful when you no longer need the data stored in the document. This could be because the data is no longer relevant, or because it has been replaced by more up-to-date information.

Important Points

  • Before deleting a document in CouchDB, make sure you have the correct ID and revision number.
  • Deleting a document is permanent and cannot be undone. Make sure you have a backup of the data if necessary.
  • When deleting documents in bulk, consider using the _bulk_docs API to optimize performance.

Summary

In this tutorial, we discussed how to delete a document in CouchDB. We covered the syntax, example, output, explanation, use, and important points of deleting a document. With this knowledge, you can now confidently delete documents from your CouchDB database when they are no longer needed.

Published on: