Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are unused indexeddb databases cleaned up

I am using IndexedDB as local storage and it is working well. For reasons that are too detailed to get into here, I often create just a single database and consume it, but in some instances I need to create more. In some of those cases, these additional databases may end up "orphaned" or unused, and unnecessary into the future. Do unused IndexedDB databases somehow "age out" of some sort of local storage cache (presumably in some browser-specific way)? I don't mind doing my own cleanup, but since there seems to be no definitive way to get a list of existing databases, I am unsure how to do it. And the ability of the user to merely delete ALL databases seems like a bit of a blunt instrument.....

I suppose I could keep a master-DB with a list of all existing databases.... but even then I don't think I can actually delete them. The best I could do is empty them I think.

Grateful for your insights.

like image 344
Stephan G Avatar asked Mar 14 '23 22:03

Stephan G


2 Answers

If you don't explicitly delete a database, generally it's going to stay there. But there are two scenarios where it will be deleted:

  1. The user deletes it. For example, in Chrome if the user clears "cookies and site data", all IndexedDB databases will be removed.

  2. The browser deletes it. Technically, the browser is allowed to delete any IndexedDB database at any time. In practice, this happens extremely rarely, possibly never. In theory it should happen when disk space is running low, but I've never seen it actually happen, even when I made an artificial test that used up all disk space in a VM.

What this means is that you can generally be pretty sure that an IndexedDB database is not going to be deleted unless you delete it, but you can't rely on that.

like image 94
dumbmatter Avatar answered Mar 24 '23 04:03

dumbmatter


if dont like that the browser delete the unused DBs under storage pressure, you can ask it to persist the storage

navigator.storage.persist()

that return a Promise

see: storage.persist definition and example

like image 39
Slimane Meharzi Avatar answered Mar 24 '23 04:03

Slimane Meharzi