PouchDB Delete Document

We can delete the document present in the Pouchdb database by the use of db. remove(). We need to pass the parameters as id and _rev value to delete the documents.

Remove() method also accepts the callback function as a parameter optionally. Instead of passing is and _rev, we can also pass entire documents.

Below is the syntax for remove() method:

db.remove( doc_Id, doc_Rev, [callback] )  

Lets see an example to delete, initially we need to read the contents of document we wish to delete.

{ 
age: 32, 
_id: '101',
 _rev: '3-59da1619d63f8db992f6233bb7dc5b2c' 
}
This database is stored in the database named second_Database 
// package we need, by using require method  
var PouchDB = require('PouchDB');  
//Creating the object of database  
var db = new PouchDB('Second_Database');  
//Deleting an existing document  
db.remove('101', '3-59da1619d63f8db992f6233bb7dc5b2c', function(err) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log("Document deleted successfully");  
   }  
});

Store the above code snippet in a file named deldoc.js.

Next, open the command prompt and execute the file by the use of node as depicted below.

C:\Users\ACER\Documents\PouchDB_Examples>node deldoc.js

This will delete the Document present in the database named second_database, which is saved locally. It will display the following message.

Document deleted successfully
PouchDB Delete Document

Output:

We can verify if the document is deleted or not by reading the document and it will show the following message:

[not_found: missing] {
  status: 404,
  error: true,
  reason: 'deleted',
  docId: '101'
}

Delete the Document from the remote database

Pouchdb allows us to delete the Document from the database, which is stored in a remote database. To perform this, we have to pass the path of the database instead of the name of the database we wish to modify the documents from.

Let's see an example of a database named employees remotely, which has a Document with the id 001. We can verify it by accessing the following URL http://127.0.0.1: 5000/_utils/.

We can delete it by the use of the remove() method.

PouchDB Delete Document

By using following code we can retrieve the content of document

// package we need, by using require method  
var PouchDB = require('PouchDB');  
//Creating the object of database
var db = new PouchDB('http://localhost:5000/employees');  
//Reading the contents of a document  
db.get('001', function(err, doc) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log(doc);  
   }  
});

After executing above code following will be the output:

{
  name: 'Ajeet',
  age: 38,
  _id: '001',
  _rev: '2-51ac082e61dcc67aa3a21f3ed4584768'
}

Next by suing the _rev and id of the docuemn we can delete it by usd of remove() method as follows:

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

// package we need, by using require method  
var PouchDB = require('PouchDB');  
//Creating the object of database 
var db = new PouchDB('http://localhost:5000/employees');  
//Deleting an existing document  
db.remove('001', '2-51ac082e61dcc67aa3a21f3ed4584768', function(err) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log("Document deleted successfully");  
   }  
}); 

tore the above code snippet in a file named delremotedoc.js.

Next, open the command prompt and execute the file by the use of node as depicted below.

C:\Users\ACER\Documents\PouchDB_Examples>node delremotedoc.js

This will delete the Document present in the database named employees, which is saved remotely. It will display the following message.

Document deleted successfully
PouchDB Delete Document

Output:

We can verify if the document is deleted or not by reading the document and it will show the following message:
{
  error: 'not_found',
  reason: 'missing',
  status: 404,
  name: 'not_found',
  message: 'missing',
  docId: '001'
}