开发者选项
还没有办法以编程方式枚举或删除数据库(尚未)。
Chrome开发人员可以导航到chrome://settings/cookies搜索并删除任何数据库Opera开发人员可以导航到opera://settings/cookies
真正删除数据库(以及其他所有内容)的唯一方法
一项新的规范说,在具有响应标头和javascript的功能中,这可能是可能的。缺点是您无法控制要删除的内容,因此除非您想清除所有内容,否则首先需要创建所有内容的备份
2.1.3。 存储参数
storage参数表示服务器希望删除与特定响应的URL的来源相关联的本地存储的数据。 这包括存储机制,例如(localStorage,sessionStorage,[INDEXEDDB],[WEBDATABASE]等),以及与切线相关的机制,例如服务工作者注册。
Js:
navigator.storage.clear({
types: [ "storage" ],
includeSubdomains: true // false by default
});
响应头:
res.header("Clear-Site-Data", "storage; includeSubdomains");
但这尚不适用于任何浏览器...
面向客户(而非开发人员)的最佳解决方案
/* This will fetch all tables from sqlite_master
* except some few we can't delete.
* It will then drop (delete) all tables.
* as a final touch, it is going to change the database
* version to "", which is the same thing you would get if
* you would check if it the database were just created
*
* @param name [string] - the database to delete
* @param cb [function] - the callback when it's done
*/
function dropDatabase(name, cb){
// empty string means: I do not care what version, desc, size the db is
var db = openDatabase(name, "", "", "");
function error(tx, err){
console.log(err);
}
db.transaction(ts => {
// query all tabels from sqlite_master that we have created and can modify
var query = "SELECT * FROM sqlite_master WHERE name NOT LIKE 'sqlite\\_%' escape '\\' AND name NOT LIKE '\\_%' escape '\\'";
var args = [];
var success = (tx, result) => {
var rows, i, n, name;
rows = result.rows;
n = i = rows.length;
// invokes cb once it’s called n times
function after(){
if (--n < 0) {
// Change the database version back to empty string
// (same as when we compear new database creations)
db.changeVersion(db.version, "", function(){}, error, cb);
}
}
while(i--){
// drop all tabels and calls after() each time
name = JSON.stringify(rows.item(i).name);
tx.executeSql('DROP TABLE ' + name, [], after, error);
}
// call it just 1 more extra time incase we didn't get any tabels
after();
};
ts.executeSql(query, args, success, error);
});
}
用法
dropDatabase("database", function(){
console.log("done")
});