10

If I go into my browser's console (I'm using Chrome) right now, on this very page, and type

indexedDB.open("MyDB").onsuccess = function(e) { console.log("success"); };

I immediately get a "success" message in my console. I can do this as many times as I like, and it works fine. But then if I type

indexedDB.deleteDatabase("MyDB").onsuccess = function(e) { console.log("success"); };

I get no "success" message back. Not only that, but if I then try and call .open again, I also get no "success" message back. How can I cure this strange illness caused by .deleteDatabase, and what exactly is happening?

(PS: Just as I finished typing this answer, I think the "success" message from the call to .deleteDatabase finally did come through, about two minutes after I made the call - but the question stands).

Jack M
  • 4,769
  • 6
  • 43
  • 67

1 Answers1

14

Every time you call indexedDB.open a new connection to the database is established. When you call deleteDatabase all those open connections will get versionchange events. They can each listen for that event and close their connections in response. That is your first option. If they do not, the request returned by indexedDB.deleteDatabase("whatever") will receive a blocked event. This is what is happening in your case. Your second option is to listen to the blocked event and close the connections there:

var request = indexedDB.deleteDatabase("MyDB");
request.onsuccess = function(e) { console.log("success"); };
request.onblocked = function(e) {
  console.log("blocked: " + e);
  // Close connections here
};
request.onerror = function(e) { console.log("error: " + e); };
dgrogan
  • 2,587
  • 1
  • 17
  • 21
  • 1
    Yep. And the reason you suddenly got "success" messages a few minutes later is that the connections you opened were finally garbage collected, unblocking the delete. And (per spec) an open request after a delete request waits until the delete is complete, which is why those later opens were silent. – Joshua Bell Feb 01 '16 at 22:53
  • 2
    When you say "close connections here", how exactly do I do that? My problem is that I don't have any references to the open connections. In fact, in my case, I get this problem even if the deleteDatabase request is the first code run when the page loads (I'm making a chrome extension). – Jack M Feb 02 '16 at 12:27
  • indexedDB.open("MyDB").onsuccess = function(e) { var db = e.target.result; db.close(); }; – Joshua Bell Feb 02 '16 at 19:06
  • 3
    So how does this event get unblocked? Waiting 2 minutes for it to finish is kind of silly. – Johann Jul 07 '17 at 15:00
  • The event gets unblocked when all the open connections have been closed. Do this by either of the options mentioned in the answer and you won't have to wait two minutes. – dgrogan Jul 23 '20 at 16:51
  • @JackM I tried both ways and still had to wait for garbage collection to happen. Can you explain on how this answer helped you? dgrogan, can you provide a full working example? – stackprotector Sep 07 '20 at 15:19