Skip to content

Instantly share code, notes, and snippets.

@rmehner
Last active April 4, 2024 09:18
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmehner/b9a41d9f659c9b1c3340 to your computer and use it in GitHub Desktop.
Save rmehner/b9a41d9f659c9b1c3340 to your computer and use it in GitHub Desktop.
Delete all indexedDB databases
// Credit to @steobrien from https://gist.github.com/rmehner/b9a41d9f659c9b1c3340#gistcomment-2940034
// for modern browsers, this works:
const dbs = await window.indexedDB.databases()
dbs.forEach(db => { window.indexedDB.deleteDatabase(db.name) })
// for older browsers, have a look at previous revisions of this gist.
@febeling
Copy link

febeling commented Mar 4, 2015

cool, better than digging through the chrome settings page. #thx

@peritus
Copy link

peritus commented Aug 19, 2015

❤️

@admbtlr
Copy link

admbtlr commented Aug 19, 2015

mad props

@ally
Copy link

ally commented Apr 21, 2016

omfg

@blubbll
Copy link

blubbll commented Jun 8, 2018

so sad that this is broken now

@steobrien
Copy link

up-to-date version:

const dbs = await window.indexedDB.databases()
dbs.forEach(db => { window.indexedDB.deleteDatabase(db.name) })

@alexmi256
Copy link

Unfortunately the above is not currently implemented in Firefox or Safari, see https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/databases

@rmehner
Copy link
Author

rmehner commented May 7, 2020

@alexmi256: Have a look at what @steobrien posted just above your comment: https://gist.github.com/rmehner/b9a41d9f659c9b1c3340#gistcomment-2940034

I'll update the main gist to reflect those changes.

@alexmi256
Copy link

The version posted by @steobrien is what I was referring to, the databases()function of indexed DB is only supported by Chrome, Edge, Opera. Safari and Firefox do not support it.

@frederikhors
Copy link

@alexmi256 you're right. Any workaround for Firefox and Safari?

@rmehner
Copy link
Author

rmehner commented Jun 12, 2020

@alexmi256 @frederikhors AFAIK there's no way to do this in Firefox.

For Safari, you might get lucky by using the previous version of this gist: https://gist.github.com/rmehner/b9a41d9f659c9b1c3340/c7971d082a04a8f47ec496e7eefa5521af2f1577

@rmehner
Copy link
Author

rmehner commented Jun 12, 2020

Just double checked, with current Safari it also not working anymore. databases() not being in the spec is probably the reason for it.

@rmehner
Copy link
Author

rmehner commented Jun 12, 2020

Is there anything wrong if I use: window.indexedDB.deleteDatabase('myDB')?

No, this is the right way to delete a indexedDB database. The code above actually used that method as well. The difference is, that the gist gets all databases in the current scope and deletes them using the line you've posted.

@rmehner
Copy link
Author

rmehner commented Jul 20, 2020

@NimaSoroush Problem is that Firefox and Safari both do not supported the databases method on the indexedDB instance and therefore this can't work. Or did something change?

@NimaSoroush
Copy link

ah right, missed that point

@jamesmfriedman
Copy link

/**
 * Polyfill for indexedDB.databases()
 * Safari and some other older browsers that support indexedDB do NOT
 * Support enumerating existing databases. This is problematic when it
 * comes time to cleanup, otherwise we could litter their device with
 * unreferenceable database handles forcing a nuclear browser clear all history.
 */

(function () {
	if (window.indexedDB && typeof window.indexedDB.databases === 'undefined') {
		const LOCALSTORAGE_CACHE_KEY = 'indexedDBDatabases';

		// Store a key value map of databases
		const getFromStorage = () =>
			JSON.parse(window.localStorage[LOCALSTORAGE_CACHE_KEY] || '{}');

		// Write the database to local storage
		const writeToStorage = value =>
			(window.localStorage[LOCALSTORAGE_CACHE_KEY] = JSON.stringify(value));

		IDBFactory.prototype.databases = () =>
			Promise.resolve(
				Object.entries(getFromStorage()).reduce((acc, [name, version]) => {
					acc.push({ name, version });
					return acc;
				}, [])
			);

		// Intercept the existing open handler to write our DBs names
		// and versions to localStorage
		const open = IDBFactory.prototype.open;

		IDBFactory.prototype.open = function (...args) {
			const dbName = args[0];
			const version = args[1] || 1;
			const existing = getFromStorage();
			writeToStorage({ ...existing, [dbName]: version });
			return open.apply(this, args);
		};

		// Intercept the existing deleteDatabase handler remove our
		// dbNames from localStorage
		const deleteDatabase = IDBFactory.prototype.deleteDatabase;

		IDBFactory.prototype.deleteDatabase = function (...args) {
			const dbName = args[0];
			const existing = getFromStorage();
			delete existing[dbName];
			writeToStorage(existing);
			return deleteDatabase.apply(this, args);
		};
	}
})();

@ray007
Copy link

ray007 commented Mar 3, 2021

So how do Firefox Web-developer tools list the databases?
There must be something for this to work.

@rmehner
Copy link
Author

rmehner commented Mar 3, 2021

@ray007 good idea, the source code of this is actually open source, maybe you find something there: https://github.com/firefox-devtools

Would be useful indeed!

@hesselbom
Copy link

Here's a condensed one-liner if you just want something to copy and paste into your browser console:
indexedDB.databases().then(dbs => dbs.forEach(db => indexedDB.deleteDatabase(db.name)))

@RobertoMaurizzi
Copy link

Safari DOES have indexedDb.databases, but can't do await from the console. Luckily the abovementioned .then(dbs => dbs.forEach...) is enough, since the oh so perfect Apple User interface requires you do delete EACH COLLECTION (instead of each database) and to do that you have to move the pointer to the other side of the window and click the trashcan button.
Firefox implements it only in webworkers so I guess it'll still be a lot of right-click-deletes (at least much faster than in Safari) unless you implement a helper function in a web worker (that HAS to be downloaded from the server).
Oh and if you think this means Chrome has a better implementation of indexedDbs, think again: it's significantly slower than both FF and Safari.... #webstandardsssuck #sorryfortherant

@epsislow
Copy link

epsislow commented Aug 3, 2021

Sadly it doesn't work in Firefox Web Workers either, even if everyone says it does.. :(

		const workFunc = function (data) {
			const idd = indexedDB
				 || webkitIndexedDB
				 || mozIndexedDB;
			
			const deletedDbs = [];
			
			idd.databases()
				.then(dbs => dbs.forEach(db => {idd.deleteDatabase(db.name); deletedDbs.push(db.name);}))
				.then(function () {self.postMessage(deletedDbs)});
		};

		worker = new Worker(URL.createObjectURL(new Blob(["onmessage =" + workFunc.toString()], {type: "text/javascript"})));

		worker.onmessage = function (deletedDbs) {
			console.log('Deleted databases: '+ deletedDbs.join(','));
		};

		worker.onerror = function (e) {
			console.log('Error? What error?', e.message, '. Oh that one!');
		};
		
		worker.postMessage('testing')

@igwejk
Copy link

igwejk commented Feb 21, 2022

Remember to wrap the execution in an async function.

(async () => {
    const dbs = await window.indexedDB.databases();
    dbs.forEach(db => { window.indexedDB.deleteDatabase(db.name) });
})();

This works fine for me on Safari. Note that you may not see the effect immediately on the dev tool until you close the browser window and open it again.

@cindywu
Copy link

cindywu commented May 3, 2022

🙏🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment