Skip to content

Instantly share code, notes, and snippets.

@rmehner
Last active May 13, 2024 13:40
Show Gist options
  • 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.
@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