5

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.

Stephan G
  • 3,289
  • 4
  • 30
  • 49

3 Answers3

8

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.

dumbmatter
  • 9,351
  • 7
  • 41
  • 80
  • Thanks.... this leaves open (of course) the question of how to "play nice" with a computer that is low on disk space. No way for the client code to know anything about the space available on the disk, so no way to be more conservative in how much of a cache to implement in IndexedDB.... so it goes I guess.; – Stephan G Sep 01 '15 at 20:22
  • 2
    Stephan - Chrome has the quota API that you can use to know how much of your origin's quota allotment is left. But for now chrome distributes quota very conservatively; feel free to make the IDBs as big as you want without worrying about affecting the machine. https://developer.chrome.com/apps/offline_storage#query – dgrogan Sep 01 '15 at 22:35
1

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

0

According to this 2020 article, Safari will delete any IndexedDB database after 7 days of inactivity.

deleting all of a website’s script-writable storage after seven days of Safari use without user interaction on the site

Perhaps this is related to Apple's preference of App Store apps over unapproved PWAs.

https://webkit.org/blog/10218/full-third-party-cookie-blocking-and-more/

https://news.ycombinator.com/item?id=28158407

cssyphus
  • 37,875
  • 18
  • 96
  • 111