IndexedDB-delete whole database by javaScript

Posted by Yuankun Li on 2019-10-28

使用indexedDB.deleteDatabase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var DBDeleteRequest = window.indexedDB.deleteDatabase("toDoList");

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
};

// you must call db.close() before delete, if you already open. otherwise you will get onblocked event
DBDeleteRequest.onblocked = function () {
console.log("Couldn't delete database due to the operation being blocked");
};

indexedDB.databases()

可以使用indexedDB.databases() 得到所有的databases List.
如果这个数据库已经open过,在删除数据库前需要调用db.close() 否则onblocked event会触发。

1
2
3
4
5
6
7
return window.indexedDB.databases()
.then((dbs) => {
dbs.map(db => {
db.close();
window.indexedDB.deleteDatabase(db.name)
})
}).catch(console.error);

indexedDB.databases() 有一个很大问题就是支持的浏览器太少了。只有Chrome和Opera能很好的支持,IE11, Firefox, Safari都不太支持。

实例:删除除了此user的所有其他IndexedDB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

/**
* Checks IndexedDB for all IndexedDBs and deletes them if the username
* is not the one provided. Used when changing users after session expires.
*
* @static
* @param {String} usernameToKeep Current authenticated user
* @returns
* @memberof DB
*/
static purgeIndexedDB(usernameToKeep) {
if (window.indexedDB && window.indexedDB.databases) {
return window.indexedDB.databases()
.then((dbs) =>
dbs.filter((db) => (db.name.includes('_pouch_') && (!usernameToKeep || !db.name.includes(usernameToKeep)))))
.then((IndexedDBS) =>
Promise.all(IndexedDBS.map((IndexedDB) => window.indexedDB.deleteDatabase(IndexedDB.name))))
.catch(console.error);
}
return false;
}

Reference Links:

Pouch



show git comment