distante
distante

Reputation: 7005

Is there a way to delete all databases (without given names) in PouchDB

I want to make a function to reset all the content and initialize my webapp. Since I have a couple of generated names databases I would like just to remove them all.

Is there a way to do something like PouchDB.destroyAll() ?

Upvotes: 3

Views: 871

Answers (3)

RamblinRose
RamblinRose

Reputation: 4963

I recommend doing it the pouchy way by using the PouchDB allDbs() plugin because it works when different db adapters have been used.

For example

try {
   const dbs = PouchDB.allDbs();
   // dbs is an array of strings, e.g. ['mydb1', 'mydb2']
   // delete the databases by name or whatever.
} catch(err) {
}

The destroy() method is handy, see Destroying a database

From a developer perspective, see Deleting your local database

Upvotes: 1

Ben Oron
Ben Oron

Reputation: 74

The IndexedDB: webkitGetDatabaseNames is removed from chrome see -> https://www.chromestatus.com/feature/5725741740195840

Agree that isn't possible using PouchDB directly you can do it using the following.


export const deletePouchDB = async() =>{
    const dbs = await window.indexedDB.databases();
    dbs.forEach(db => {
        if (db.name.includes('_pouch_')) {
            window.indexedDB.deleteDatabase(db.name);
        }
    });
};

Upvotes: 0

Kul
Kul

Reputation: 1239

It's not possible using PouchDB directly but you can do it using the following snippet:

indexedDB.webkitGetDatabaseNames().onsuccess = (e) => {
  for(let i = 0; i < e.target.result.length; i++){
    let db = e.target.result[i];
    if(db.startsWith('_pouch_')){
      indexedDB.deleteDatabase(db);
    }
  }
};

This assumes that you're using PouchDB with indexedDB in chrome. If this is not the case, you'll have to adapt the above code to work with whatever storage engine and browser/server you're using.

Upvotes: 1

Related Questions