大网数据广告活动
Web 标准与前端开发

IDBFactory: deleteDatabase() method

· 52CE 我爱测

The **`deleteDatabase()`** method of the IDBFactory interface requests the deletion of a database. The method returns an IDBOpenDBRequest object immediately, and performs the deletion operation asynchronously.

IndexedDB

The deleteDatabase() method of the IDBFactory interface requests the deletion of a database. The method returns an IDBOpenDBRequest object immediately, and performs the deletion operation asynchronously.

Syntax

deleteDatabase(name)
// Non-standard
deleteDatabase(name, options)

Parameters

  • name
    • : The name of the database you want to delete. Note that attempting to delete a database that doesn't exist does not throw an exception, in contrast to IDBDatabase.deleteObjectStore(), which does throw an exception if the named object store does not exist.
  • options
    • : In Gecko, since version 26, you can include a non-standard optional storage parameter that specifies whether you want to delete a permanent (the default value) indexedDB, or an indexedDB in temporary storage (a.k.a. shared pool.)

Return value

An IDBOpenDBRequest on which subsequent events related to this request are fired.

If the operation is successful, the value of the request's IDBRequest.result property is undefined.

Description

If the database is successfully deleted, then a success event is fired on the request object returned from deleteDatabase(), with its result set to undefined. If an error occurs during deletion, an error event is fired on the request object returned from this method.

When deleteDatabase() is called, any other open connections to this particular database are sent a versionchange event, allowing them to close so that the deletion can proceed.

If a connection is not closed in response to the versionchange event, the deletion is blocked: the request's success event does not fire, and a blocked event is fired on the request instead. The deletion stays pending until every connection to the database is closed. In the meantime, connections that are still open keep working as normal: they can still run transactions until they are closed. Any later IDBFactory.open() or deleteDatabase() request for the same database is queued behind the pending deletion, and only proceeds once the deletion has completed.

Examples

Basic usage

const dbDeleteRequest = indexedDB.deleteDatabase("toDoList");

dbDeleteRequest.onerror = (event) => {
  console.error("Error deleting database.");
};

dbDeleteRequest.onsuccess = (event) => {
  console.log("Database deleted successfully");

  console.log(dbDeleteRequest.result); // undefined
};

Handling a blocked deletion

Imagine two browser tabs sharing access to the same database. The first tab requests deletion, but the second does not respond by closing the database. A blocked event fires on the request in the first tab. In this case, you may show the user a warning asking them to close the other tabs.

const dbDeleteRequest = indexedDB.deleteDatabase("toDoList");

dbDeleteRequest.onblocked = (event) => {
  console.warn(
    "Deletion is blocked: close other tabs that have this app open to continue.",
  );
};

dbDeleteRequest.onsuccess = (event) => {
  console.log("Database deleted successfully");
};

Usually, you should just handle it automatically by calling IDBDatabase.close() from inside the versionchange event handler in the second tab:

// db is an open connection (e.g. from a previous indexedDB.open() success)
db.addEventListener("versionchange", () => {
  db.close();
});

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。