Delete Batch in PouchDB

PouchDB Delete Batch
To delete a PouchDB batch, the bulkDocs() method is used.

Example:

  • Retrieve the values of a bulk document.
    Output:

    [ {  id: '101',
    key: '101',
    value: { rev: '1-f56789f432154adb782da98358921a14' },
    doc:
    { name: 'Tom',
    salary: 25000,
    experience: 3,
    _id: '101',
    _rev: '1-f56789f432154adb782da98358921a14' } },
    { id: '102',
    key: '102',
    value: { rev: '1-3cacb3a45e5678d90870f4e92dc9876d' },
    doc:
    {name: 'Jerry',
    salary: 20000,
    experience: 2,
    _id: '102',
    _rev: '1-3cacb3a45e5678d90870f4e92dc9876d' } },
    { id: '103',
    key: '103',
    value: { rev: '1-afec1229a76543db973ace8fcbacd45' },
    doc:
    { name: 'Bruno',
    salary: 30000,
    experience: 4,
    _id: '103',
    _rev: '1-afec1229a76543db973ace8fcbacd45' } } ]
  • Using the respective _id and _rev values of the document to delete them.
  • Create a file named “DeleteBatch.js” within a folder named “Examples”.
  • Add the below code to the file.
    DeleteBatch.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    docs = [{_id : '101', _rev:'1-f56789f432154adb782da98358921a14', _deleted : true },
    {_id : '102', _rev: '1-3cacb3a45e5678d90870f4e92dc9876d', _deleted : true },
    {_id : '103', _rev: '1-afec1229a76543db973ace8fcbacd45', _deleted : true }]
    db.bulkDocs(docs, function(err, response) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Batch Successfully Deleted.");
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node DeleteBatch.js
    

    Console Output:

    Batch Successfully Deleted.

 

Verification:

Check for the deleted document in the ‘Example_Database’ database in PouchDB.

 

To delete a Batch from Remote Database:

Instead of the database name, pass the path of the database to delete a batch in CouchDB or remote server.

Example:

  • Retrieve the values of a bulk document.
    Output:

    [ {  id: '101',
    key: '101',
    value: { rev: '1-acb312f4321546901aada9835889ccca1' },
    doc:
    { name: 'Tom',
    salary: 25000,
    experience: 3,
    _id: '101',
    _rev: '1-acb312f4321546901aada9835889ccca1' } },
    { id: '102',
    key: '102',
    value: { rev: '1-1a2c3b3a45ae5111d90870f4e92dc55543' },
    doc:
    {name: 'Jerry',
    salary: 20000,
    experience: 2,
    _id: '102',
    _rev: '1-1a2c3b3a45ae5111d90870f4e92dc55543' } },
    { id: '103',
    key: '103',
    value: { rev: '1-345ac229a76543db6ab5648fc987bbb5' },
    doc:
    { name: 'Bruno',
    salary: 30000,
    experience: 4,
    _id: '103',
    _rev: '1-345ac229a76543db6ab5648fc987bbb5' } } ]
  • Using the respective _id and _rev values of the document to update them.
  • Create a file named “DeleteRemoteBatch.js” within a folder named “Examples”.
  • Add the below code to the file.
    DeleteRemoteBatch.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('http://localhost:5984/students');
    docs = [{_id : '101', _rev:'1-345ac229a76543db6ab5648fc987bbb5', _deleted : true },
    {_id : '102', _rev:'1-1a2c3b3a45ae5111d90870f4e92dc55543', _deleted : true },
    {_id : '103', _rev: '1-acb312f4321546901aada9835889ccca1', _deleted : true }]
    db.bulkDocs(docs, function(err, response) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Batch Successfully Deleted.");
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node DeleteRemoteBatch.js
    

    Console Output:

    Batch Successfully Deleted.

    Verification:

    Check for the deleted document in the ‘students’ database in CouchDB.