Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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})
like image 361
K20 Avatar asked Apr 26 '15 12:04

K20


2 Answers

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!
});

```

like image 75
nlawson Avatar answered Nov 16 '22 04:11

nlawson


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);
});
like image 7
Dominik Ehrenberg Avatar answered Nov 16 '22 04:11

Dominik Ehrenberg