1

Is there any way to remove indexedDB from chrome console for cordova application if database name is LokiCatalog?

Vadim Kovrizhkin
  • 1,575
  • 4
  • 16
  • 27

2 Answers2

1

Yes you can delete it using indexeddb api "deleteDatabase". So the code to delete the database "LokiCatalog will be" -

var DBDeleteRequest = window.indexedDB.deleteDatabase("LokiCatalog");

DBDeleteRequest.onerror = function(event) {
  console.log("Error deleting database.");
};

DBDeleteRequest.onsuccess = function(event) {
  console.log("Database deleted successfully");

  console.log(event.result); // should be undefined
};

For more you can take a look here - https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/deleteDatabase

If you dont want to deal with indexeddb apis, you can use some indexeddb wrapper like - JsStore.

Ujjwal Kumar Gupta
  • 2,308
  • 1
  • 21
  • 32
  • It did not help. Database still exists. I think this is because indexedDB is wrapped with lokijs and the name if database is not LokiCatalog. But I am not sure :( – Vadim Kovrizhkin Oct 25 '17 at 07:31
  • If db name is not the one you are deleting that means db still exist, so is the data. Can you open the database with the same name, you want to delete and get all data from a particular table ? This will make sure - that db name is same or not. – Ujjwal Kumar Gupta Oct 25 '17 at 09:46
  • I found the problem. Your solution is good. Thanks. The problem is that the db is blocked by something. – Vadim Kovrizhkin Oct 25 '17 at 11:43
1

indexedDB.deleteDatabase can delete an IndexedDB database, just make sure you are listening for all events, so you get notified if the DB is blocked or else.

Here's an example of a promisified deleteDatabase version that listens to all events.

function deleteDb(dbName) {
    return new Promise((resolve, reject) => {
        const deleteDbRequest = indexedDB.deleteDatabase(dbName);
        deleteDbRequest.addEventListener('error', (e) => {
            console.log('delete error', e);
            reject(e);
        });
        deleteDbRequest.addEventListener('blocked', (e) => {
            console.log('delete blocked', e);
            reject(e);
        });
        deleteDbRequest.addEventListener('upgradeneeded', (e) => {
            console.log('delete upgradeneeded', e);
            reject(e);
        });
        deleteDbRequest.addEventListener('success', (e) => {
            console.log('delete success', e);
            resolve(e);
        });
    })
}

This is just an example. You may want to behave differently (i.e. not necessarily reject) on the upgradeneeded event, for instance.

Example usage:

deleteDb('LokiCatalog')
    .then(r => console.log('success', r))
    .catch(e => console.error('oops', e));
acdcjunior
  • 132,397
  • 37
  • 331
  • 304