PouchDB - Delete Document



You can delete a document from a database that exists in PouchDB using the db.remove() method.

Syntax

Following is the syntax of using the db.remove() method of PouchDB. To this method, we have to pass id and _rev to delete an existing document as shown in the following code. This method accepts an optional callback function. We can also pass the complete document instead of id and _rev.

db. get ( docId, docRev, [callback] )
or
db. get ( docId, docRev, [callback] )

Example

Assume we have a document in PouchDB with id 001 which have the details of a person. In order to delete this document along with its id we should also have its _rev number. Therefore, retrieve the contents of the document as shown in the following code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = new PouchDB('my_database');

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

Executing the above code gives the following output.

{
   _id: '001',
   _rev: '3-552920d1ca372986fad7b996ce365f5d',
   name: 'Raju',
   age: 23,
   designation: 'Designer' 
}

Now, using the _rev and id of the document you can delete this by using the remove() method as shown in the following code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = new PouchDB('my_database');

//Deleting an existing document
db.remove('001', '3-552920d1ca372986fad7b996ce365f5d', function(err) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document deleted successfully");
   }
});

Save the above code in a file with the name Delete_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:\PouchDB_Examples >node Delete_Document.js

This deletes the contents of the given document that exists in the database named my_database which is stored locally. The following message is displayed.

Document deleted successfully

Deleting a Document from a Remote Database

You can also delete an existing document from the database that is stored remotely on the server (CouchDB).

To do so, instead of a database name, you need to pass the path to the database in CouchDB, which contains the document that is to be read.

Example

Suppose there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB using the URL http://127.0.0.1:5984/_utils/index.html you will get the following screenshot.

Deleting a Document from a Remote Database

By clicking on the database named my_database you can see the following screenshot. Here, you can observe that the database contains a document with id 001.

Deleting Database

Following is an example of deleting the contents of the document having id “001” that exists in a database named my_database which is stored in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');

//Deleting an existing document
db.remove('001', '3-552920d1ca372986fad7b996ce365f5d', function(err) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document deleted successfully");
   }
});

Save the above code in a file with name Remote_Delete_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:\PouchDB_Examples >node Remote_Delete_Document.js

This deletes the given document that exists in the database named my_database which is stored in CouchDB. The following message is displayed.

Document deleted successfully
Advertisements