Delete Attachment in PouchDB

PouchDB Delete Attachment
To delete an attachment from PouchDB, the removeAttachment() method is used.

Syntax:

db.removeAttachment ( doc_Id, attachment_Id, rev, [callback] );

Example:

  • Create a file named “DeleteAttach.js” within a folder named “Examples”.
  • Add the below code to the file.
    DeleteAttach.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    db.removeAttachment('101', 'attach_1.txt', '1-f56789f432154adb782da98358921a14',
    function(err, res) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Attachment Successfully Deleted.")
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node DeleteAttach.js
    

    Console Output:

    Attachment Successfully Deleted.

Verification:

Use read command to verify that the attachment is deleted from the document.

To delete an Attachment from a Remote Database:

Instead of the database name, pass the path of the database to delete an attachment from CouchDB or remote server.
Example:

  • Create a file named “DeleteRemoteAttach.js” within a folder named “Examples”.
  • Add the below code to the file.
    DeleteRemoteAttach.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('http://localhost:5984/students');
    db.removeAttachment('101', 'attach_2.txt', '2-fec1229a76543db973ace8fcbacd453',
    function(err, res) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Attachment Successfully Deleted.")
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node DeleteRemoteAttach.js
    

    Console Output:

    Attachment Successfully Deleted.

Verification:

Check the document in the CouchDB server for the deleted attachment. There will be no attachment as such.