Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all indexedDB

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)

like image 429
Felipe Pincheira Avatar asked Sep 04 '17 15:09

Felipe Pincheira


2 Answers

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.');
});
like image 104
Yash Bora Avatar answered Nov 09 '22 14:11

Yash Bora


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/
like image 41
Rohan Fating Avatar answered Nov 09 '22 14:11

Rohan Fating