2

Is there anything like ondelete event handler, or way to warn user before they want to clear the indexedDB database.

Background: There is a offline first app, that works by saving most of the data on indexedDB while offline. Now, when the user goes online, it tries to sync the data to the server, but, now, if someone mistakenly resets browser or clears the indexedDB, they might create a disaster, since they have important data that needs to sync to server.

So, is there any way to overcome this problem? Is there a way to save indexedDB as a persistent file system or disable deletion or raise an event and handle properly when there is a deletion?

user3330840
  • 6,143
  • 7
  • 26
  • 39

1 Answers1

4

No. Despite how some of us are using it, there is no guarantee that data in an IndexedDB database will remain there. This is by design, it is "temporary" storage. The browser can free up space if it needs to. The situation will be somewhat improved when eventually it is possible to store IndexedDB data as "persistent" data, but there's never going to be a way to guarantee data isn't deleted. IMHO the user should have the ability to delete data on their computer and it would be a significant problem if that wasn't true.

And the problem may be even worse than you realize. For instance, here's a bug in Chrome (and maybe in other browsers too). If the user says "delete my cookies and site data from the past 24 hours", Chrome will completely delete any IndexedDB database that has been changed in the past 24 hours. So if you have 5000 objects in a DB that were created a year ago but never changed since, and one that was edited today, they all get deleted. This can be confusing for users, but to my knowledge nobody has proposed a good solution.

The only workaround is to sync to the server, which obviously is not 100% reliable. So if you have any data that is super important, it probably shouldn't be in a website's IndexedDB database except as a cache.

dumbmatter
  • 9,351
  • 7
  • 41
  • 80