pouchdb delete allDocs javascript
Asked Answered
N

6

19

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})
Nevski answered 26/4, 2015 at 12:26 Comment(0)
B
28

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

```

Basrelief answered 27/4, 2015 at 4:5 Comment(6)
Does this code work: db.allDocs()..then(function (response) { var doc; for(doc in response.rows) { return db.remove(doc); } });Nevski
You need to use Promise.all() instead of a for-loop - otherwise you will only delete the first one. I highly recommend the video from this guide: pouchdb.com/guides/async-code.htmlBasrelief
Sorry for the late reply. About how to improve the understanding of the webside, I don't really know but I think you overestimate the knowledge of the user. I really new in pouchdb and I spend a lot of time making researching after reading something on your website. Also the samples are made for really specific case, when I want to do something a little different I am completely lost. And it's maybe an impression but I think that all the lexicon is not describe.Nevski
I try really hard to make PouchDB newbie-friendly, but some of the concepts are difficult, it's true. :( Sorry I couldn't make it easier for you. If you try using ES7 with Babel, you may find asynchronous programming to be much more beginner-friendly: pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.htmlBasrelief
Just in case someone stumbles across this code in the future. @nlawson's code works, it is just missing one more ) that closes the .all( function call. Here is the code for your convenience: db.allDocs().then(function (result) { return Promise.all( result.rows.map( function (row) { return db.remove(row.id, row.value.rev); })); }).then( ...Cordelier
This is the best answer ! Thanks !Coupe
T
7

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);
});
Trista answered 6/7, 2017 at 11:10 Comment(0)
M
1

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()
Mcqueen answered 26/3, 2021 at 14:3 Comment(0)
P
0

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");
            }
        });
    });
});
Purpure answered 30/11, 2016 at 6:23 Comment(0)
V
0
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');
    }

  })
})
Vicarage answered 28/6, 2022 at 7:26 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Arette
P
-1

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);
    });
  }
}
Pattiepattin answered 24/3, 2017 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.