- PouchDB - Miscellaneous
- PouchDB - Synchronization
- PouchDB - Replication
- PouchDB - Deleting Attachment
- PouchDB - Retrieving Attachment
- PouchDB - Adding Attachment
- PouchDB - Delete Batch
- PouchDB - Update Batch
- PouchDB - Fetch Batch
- PouchDB - Create Batch
- PouchDB - Delete Document
- PouchDB - Update Document
- PouchDB - Read Document
- PouchDB - Create Document
- PouchDB - Delete Database
- PouchDB - Database Info
- PouchDB - Create Database
- PouchDB - Environment
- PouchDB - Overview
- PouchDB - Home
PouchDB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
PouchDB - Delete Batch
You can delete an array of documents in PouchDB at once using the bulkDocs() method. To do so you need to create an array of documents that are to be deleted where, each document should contain _id and _rev. In addition to these you have to add another key-value pair _deleted: true.
Suppose the database named my_database that is stored locally in PouchDB contains 3 documents namely doc1, doc2, doc3 with the following contents.
doc1 = {_id: 001 , name: Ram , age: 23, Designation: Programmer } doc2 = {_id: 002 , name: Robert , age: 24, Designation: Programmer } doc3 = {_id: 003 , name: Rahim , age: 25, Designation: Programmer }
And say, we have to delete all the three documents. Then, first of all you need to get their _rev values. Therefore, fetch the contents of these documents using the following code.
//Requiring the package var PouchDB = require( PouchDB ); //Creating the database object var db = new PouchDB( my_database ); //Retrieving all the documents in PouchDB db.allDocs({include_docs: true},function(err, docs) { if (err) { return console.log(err); } else { console.log(docs.rows); } });
Save the above code as bulk_fetch.js. Executing the above program gives you the _id and _rev values of the documents in the database as shown below.
[ { id: 001 , key: 001 , value: { rev: 1-1604b0c3ff69dc1e261265fd60808404 } }, { id: 002 , key: 002 , value: { rev: 1-b5e49db7e984841bf12a13e3ee548125 } }, { id: 003 , key: 003 , value: { rev: 1-a7b342786ecc707aa91f3b321a177b51 } } ]
Now, you can delete the documents using their respective _id and _rev values as shown below.
//Requiring the package var PouchDB = require( PouchDB ); //Creating the database object var db = new PouchDB( my_database ); //Preparing the document docs = [{_id : 001 , _rev: 2-77f3a9974dd578d12f3f2a33aae64c8d , _deleted : true }, {_id : 002 , _rev: 2-43966007568ce9567c96422195fcfa0d , _deleted : true }, {_id : 003 , _rev: 2-6c5349652527f4f39583ff14f23cd677 ,_deleted : true }] //Deleting Documents db.bulkDocs(docs, function(err, response) { if (err) { return console.log(err); } else { console.log(response+"Documents deleted Successfully"); } });
Save the above code in a file with the name Delete_All_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.
C:PouchDB_Examples >node Delete_All_Document.js
This deletes all the documents that exists in the database named my_database which is stored locally, displaying the following message.
Documents Deleted Successfully
Now, if you execute the bulk_fetch.js program, you can observe an empty brace on the console indicating that the database is empty, as shown below.
[]
Deleting Batch from a Remote Database
You can update all the documents 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 pst of databases in CouchDB using the URL http://127.0.0.1:5984/_utils/index.html you will get the following screenshot.
If we select the database named my_database, you can observe that it contains 3 documents as shown in the following screenshot.
Following is an example of deleting all the documents that exist 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 ); //Preparing the document docs = [{_id : 001 , _rev: 4-6bc8d9c7a60fed2ed1667ec0740c1f39 , _deleted : true }, {_id : 002 , _rev: 2-1aa24ce77d96bb9d2a0675cdf1e113e0 , _deleted : true }, {_id : 003 , _rev: 2-fa113149ba618eda77f73072974a2bc1 ,_deleted : true }] //Deleting Documents db.bulkDocs(docs, function(err, response) { if (err) { return console.log(err); } else { console.log("Documents deleted Successfully"); } });
Save the above code in a file with name Remote_delete_AllDocuments.js. Open the command prompt and execute the JavaScript file using node as shown below.
C:PouchDB_Examples >node Remote_Delete_AllDocuments.js
This deletes the contents of all given document that exists in the database named my_database which is stored in CouchDB, and displays the following message.
Documents Deleted SuccessfullyAdvertisements