13

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)

3 Answers 3

20

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.'); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
I have probably come back to this answer a hundred times… Why doesn't Chrome have this functionality built in? I should probably save this script somewhere.
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/ 

5 Comments

thanks for your answer, but I want to delete without knowing the names of IndexedDB and I need to delete from code.
@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.
thanks Josh, one of my options was to add to localStorage, I can't see too much possibilities.
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 - github.com/w3c/IndexedDB/issues/31 - but that doesn't help you right now.
@JoshuaBell I saved the Indexed db names in the localstorage, but I read about other options for the future. thanks for you answer.
3
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); }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.