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})
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
K20
  • 197
  • 1
  • 6

6 Answers6

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

```

slhck
  • 36,575
  • 28
  • 148
  • 201
nlawson
  • 11,510
  • 4
  • 40
  • 50
  • Does this code work: db.allDocs()..then(function (response) { var doc; for(doc in response.rows) { return db.remove(doc); } }); – K20 Apr 27 '15 at 21:12
  • 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: http://pouchdb.com/guides/async-code.html – nlawson Apr 27 '15 at 21:26
  • 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. – K20 May 03 '15 at 22:46
  • 1
    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: http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html – nlawson May 03 '15 at 23:41
  • 3
    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( ...` – Uzbekjon Apr 02 '16 at 19:06
  • This is the best answer ! Thanks ! – Alexie01 Sep 03 '21 at 12:11
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);
});
Dominik Ehrenberg
  • 1,533
  • 1
  • 15
  • 12
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()
Balaji
  • 9,657
  • 5
  • 47
  • 47
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");
            }
        });
    });
});
MFAL
  • 1,090
  • 13
  • 19
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');
    }

  })
})
  • 2
    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](/help/how-to-answer). – Community Jun 28 '22 at 10:23
-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);
    });
  }
}
Isaac Obella
  • 2,613
  • 2
  • 16
  • 29