12

I need to delete all my IndexedDB, currently I have:

const indexedDB = window.indexedDB || window.mozIndexedDB
  || window.webkitIndexedDB || window.msIndexedDB;
if (indexedDB.webkitGetDatabaseNames) {
  const bases = indexedDB.webkitGetDatabaseNames();
  bases.onsuccess = (event) => {
    const data = event.target.result;
    Object.values(data).forEach((db) => {
      indexedDB.deleteDatabase(db);
    });
    resolve();
  };
  bases.onerror = reject;
}

But the webkitGetDatabaseNames() function is undefined. Is it possible delete all IndexedDB without use this method?

PD: I want to delete without knowing the names of IndexedDB and I need to delete from code (Javascript)

Felipe Pincheira
  • 442
  • 1
  • 6
  • 21

3 Answers3

18

use on chrome

window.indexedDB.databases().then((r) => {
    for (var i = 0; i < r.length; i++) window.indexedDB.deleteDatabase(r[i].name);
}).then(() => {
    alert('All data cleared.');
});
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Yash Bora
  • 179
  • 1
  • 3
  • Thanks. This worked perfectly! Note that for `indexedDB` usage inside a `ServiceWorker`, use `self` for the global scope (instead of `window`). Actually, `self` works outside the `ServiceWorker` as well. In case you want to automatically reopen the deleted database, add `self.indexedDB.open('your_db_name', 0);` in the final `then` clause. – OXiGEN Jul 14 '23 at 15:44
6

Function call for getting names indexedDB.webkitGetDatabaseNames is deprecated. See the reference below:

https://github.com/dfahlander/Dexie.js/issues/532

There is also an intent to deprecate: IDBFactory webkitGetDatabaseNames

https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/2fUr-3wFPKI/discussion

You can use following techinques to delete the database:

Technique 1:

As far as I can tell, one should use indexedDB.deleteDatabase:

var req = indexedDB.deleteDatabase(databaseName);
req.onsuccess = function () {
    console.log("Deleted database successfully");
};
req.onerror = function () {
    console.log("Couldn't delete database");
};
req.onblocked = function () {
    console.log("Couldn't delete database due to the operation being blocked");
};

I can confirm that it works with PhantomJS 1.9.0 and Chrome 26.0.1410.43.

Technique 2

In theory, all you need to do to delete an IndexedDB in Chrome is:

  1. In Chrome, go to Options > Under the Hood > Content Settings > All cookies and Site Data > find the domain where you created the IndexedDB
  2. Hit either the "X" or click "Indexed Database" > Remove

In Windows, the file is located here:

%USERPROFILE%\AppData\Local\Google\Chrome\User Data\Default\IndexedDB

On Mac, do the following:

  1. In Chrome, go to "Settings" (or "Preferences" under the Chrome menu)
  2. Click "show advanced settings" (at the bottom of the page)
  3. Go to "Privacy" > "Content Settings" > "All cookies and Site Data" > find the domain where you created the IndexedDB
  4. Hit either the "X" or click "Indexed Database" > Remove

On Mac, the folder is located here:

/Users/[USERNAME]/Library/Application Support/Google/Chrome/Default/IndexedDB/

On Linux, the folder is located at:

/home/[USERNAME]/.config/google-chrome/Default/IndexedDB/
Rohan Fating
  • 2,135
  • 15
  • 24
  • 2
    thanks for your answer, but I want to delete without knowing the names of IndexedDB and I need to delete from code. – Felipe Pincheira Sep 04 '17 at 15:45
  • 1
    @FelipePincheira the ability to delete without knowing the database names was removed from indexedDB. You can try tracking the databases as you create them (e.g. in localStorage), or rethink how to accomplish whatever is your larger goal. – Josh Sep 05 '17 at 00:47
  • thanks Josh, one of my options was to add to localStorage, I can't see too much possibilities. – Felipe Pincheira Sep 05 '17 at 13:33
  • 1
    Some libraries that abstract away the API create a dedicated Indexed DB database specifically for tracking the other databases created via the library. There has been interest by other browser vendors in implementing a standardized way of providing a database enumeration mechanism - https://github.com/w3c/IndexedDB/issues/31 - but that doesn't help you right now. – Joshua Bell Sep 05 '17 at 21:27
  • @JoshuaBell I saved the Indexed db names in the localstorage, but I read about other options for the future. thanks for you answer. – Felipe Pincheira Sep 06 '17 at 14:53
0
indexedDB.databases().then(dbs => {
    var promises = dbs.map(db => {
        return new Promise((resolve, reject) => {
            var req = indexedDB.deleteDatabase(db.Name);
            req.onsuccess = resolve;
            req.onerror = reject;
            req.onblocked = reject;
        });
    });
    Promise.all(promises).then(console.log).catch(console.error);
})
AminFarajzadeh
  • 346
  • 5
  • 17