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})
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!
});
```
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With