IDBFactory: deleteDatabase() method
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is available in Web Workers.
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.
If the database is successfully deleted, then a success event is fired on
the request object returned from this method, with its result set to
undefined. If an error occurs while the database is being deleted, then an
error event is fired on the request object that is returned from this
method.
The deletion does not complete while other connections to the database are still open.
When deleteDatabase() is called, any other open connections to this particular database
are sent a versionchange event,
giving them the opportunity to close so that the deletion can proceed.
Note:
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. To let it complete, close each connection — for example by calling
IDBDatabase.close(), typically from a versionchange event handler.
Syntax
// For the current standard:
deleteDatabase(name)
// For the experimental version with `options` (see below):
deleteDatabase(name)
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. optionsOptional-
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 intemporarystorage (aka 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 result property is null.
Examples
const DBDeleteRequest = window.indexedDB.deleteDatabase("toDoList");
DBDeleteRequest.onerror = (event) => {
console.error("Error deleting database.");
};
DBDeleteRequest.onsuccess = (event) => {
console.log("Database deleted successfully");
console.log(event.result); // should be undefined
};
Specifications
| Specification |
|---|
| Indexed Database API 3.0> # ref-for-dom-idbfactory-deletedatabase①> |
Browser compatibility
See also
- Using IndexedDB
- Starting transactions:
IDBDatabase - Using transactions:
IDBTransaction - Setting a range of keys:
IDBKeyRange - Retrieving and making changes to your data:
IDBObjectStore - Using cursors:
IDBCursor - Reference example: To-do Notifications (View the example live).