PouchDB Delete Document



PouchDB Delete Document

  • db.remove() - It is used to delete a document from PouchDB database. You need to pass id and _rev value to delete an existing document.
  • This method takes an optional parameter is callback function.
  • Additionally, you can pass the completed document instead of id and _rev.
 PouchDB Delete Document:

PouchDB Delete Document

Syntax:

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

Delete Document Example

To retrieve the value of document which you need to delete by using Read Document method.


{ age: 25,
 _id: '001', 
_rev: '2-2da06e94be54c8390eb02435a15e9a68'
 }
  • This document is stored in a database named "Second_Database" in PouchDB.
 PouchDB Delete Document:

PouchDB Delete Document

  • To use remove() method with _rev value and id of the document.
//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('Second_Database');  
//Deleting an existing document  
db.remove('001', '2-2da06e94be54c8390eb02435a15e9a68', function(err) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log("Document deleted successfully");  
   }  
});  
  • Save the document named "Delete_Document.js" within a folder name "PouchDB_Examples". Open the command prompt and Execute the JavaScript file:
node Delete_Document.js  

Output:

 PouchDB Delete Document:

PouchDB Delete Document

Verification

  • You'll be able confirm that your record is erased or not by recovering your document. In case it is erased it'll appear message:
 PouchDB Delete Document:

PouchDB Delete Document

  • You can see that document is deleted.

Delete a Document from Remote Database

You can also delete an existing document in a remotely stored database(CouchDB). For this purpose, You just pass the path of the database which contains the documents that you need to delete.

Example

  • We have a database named "employees" on the CouchDB Server.
 PouchDB Delete Document:

PouchDB Delete Document

  • The employee database has a document having id "001".

Let's delete the above document.

//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('http://localhost:5984/employees');  
//Deleting an existing document  
db.remove('001', '2-7258b5c92fedd3894ca53f15eb647e39', function(err) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log("Document deleted successfully");  
   }  
});  
  • Save the above code in a file named "Delete_Remote_Document.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:
node Delete_Remote_Document.js  

Output:

 PouchDB Delete Document:

PouchDB Delete Document

Verification

  • Check CouchDB server. There is no document in "employee" database.
 PouchDB Delete Document:

PouchDB Delete Document



Related Searches to PouchDB Delete Document: