Search code examples
javascripthtmlphantomjsindexeddb

How can I remove a whole IndexedDB database from JavaScript?


How can one remove a whole IndexedDB database from JavaScript, as opposed to just an object store? I'm using the IndexedDB shim, which may use WebSQL as its backend.

I'd mainly like to know how to do this for the PhantomJS (headless) browser, although Chrome, Safari (on iPad) and IE10 are other important browsers.


Solution

  • As far as I can tell, one should use indexedDB.deleteDatabase:

    var req = indexedDB.deleteDatabase(databaseName);
    req.onsuccess = function () {
        console.log("Deleted database successfully");
    };
    req.onerror = function () {
        console.log("Couldn't delete database");
    };
    req.onblocked = function () {
        console.log("Couldn't delete database due to the operation being blocked");
    };
    

    I can confirm that it works with PhantomJS 1.9.0 and Chrome 26.0.1410.43.