K20
K20

Reputation: 197

pouchdb delete allDocs javascript

I am new in pouchdb and I can't understand the API.

I want to know what is the best way to delete all documents with a javascript code. I try many things but nothing seams to work.

Do I have to use some options in the allDocs method like:

db.allDocs({include_docs: true, deleted: true})

Upvotes: 19

Views: 10347

Answers (6)

Samuel saah
Samuel saah

Reputation: 1

db.allDocs({ include_docs: true }).then(response => {
  let documents = response.rows.map(res => {
    return {
      _id: res.id,
      _rev: res.value.rev,
      _deleted: true
    }
  });

  db.bulkDocs(documents, (err, response) => {
    if (!err) { console.log('Documents deleted successfully!'); } else {
      console.log('Documents could not be deleted');
    }

  })
})

Upvotes: 0

Balaji
Balaji

Reputation: 10887

You can destroy and create it again

use destroy()

var db = new PouchDB('mydb');

var reset = function() {
  db.destroy().then(function() {
    db = new PouchDB('mydb');
  });
};

to Reset

reset()

Upvotes: 1

Dominik Ehrenberg
Dominik Ehrenberg

Reputation: 1746

Based on nlawson's Answer you can also use bulkDocs, so you don't have to run a Pouch operation for every document:

db.allDocs({include_docs: true}).then(allDocs => {
  return allDocs.rows.map(row => {
    return {_id: row.id, _rev: row.doc._rev, _deleted: true};
  });
}).then(deleteDocs => {
  return db.bulkDocs(deleteDocs);
});

Upvotes: 7

Isaac Obella
Isaac Obella

Reputation: 2643

It would be easy if you used the pouchdb-erase npm package.The link shows how to use it in nodejs, however I have used it in angular 2 and it works like a charm.Here is some of my code.

import * as PouchDB from 'pouchdb';
@Injectable()
export class DBProvider {
  private _db;
  private _data;

  constructor() {
    window["PouchDB"] = PouchDB;//Debugging
    PouchDB.plugin(require('pouchdb-erase'));
  }

truncate(){
    this._db.erase().then(res=>{
        console.log(res);
    }).catch(err=>{
console.log(err);
    });
  }
}

Upvotes: -1

MFAL
MFAL

Reputation: 1110

If promises are not available you could use callbacks and a counter if you care to be notified that all rows have been removed.

db.allDocs().then(function(_response){
    var toBeDeleted = _response.rows.length;
    _response.rows.forEach(function(row){
        db.remove(row.id, row.value.rev, function(err, success){
            if(err){
                console.error(err);
            }
            else if(success){
                console.log("document with id %s was deleted", row.id);
            }
            if(--toBeDeleted == 0){
                console.log("done");
            }
        });
    });
});

Upvotes: 0

nlawson
nlawson

Reputation: 11620

Sorry the API is so confusing! If you can let us know how to improve it, that would be helpful. :)

You can either do db.destroy(), which completely erases the database but does not replicate the deletions, or you can individually remove() all documents:

db.allDocs().then(function (result) {
  // Promise isn't supported by all browsers; you may want to use bluebird
  return Promise.all(result.rows.map(function (row) {
    return db.remove(row.id, row.value.rev);
  }));
}).then(function () {
  // done!
}).catch(function (err) {
  // error!
});

```

Upvotes: 28

Related Questions