Dieser Inhalt wurde automatisch aus dem Englischen übersetzt, und kann Fehler enthalten. Erfahre mehr über dieses Experiment.

View in English Always switch to English

IDBObjectStore: clear() Methode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨Juli 2015⁩.

Hinweis: Diese Funktion ist in Web Workers verfügbar.

Die clear()-Methode des IDBObjectStore-Interfaces erstellt und gibt sofort ein IDBRequest-Objekt zurück und leert diesen Objekt-Store in einem separaten Thread. Dies dient dazu, alle aktuellen Daten aus einem Objekt-Store zu löschen.

Das Leeren eines Objekt-Stores besteht darin, alle Datensätze aus dem Objekt-Store zu entfernen und alle Datensätze in Indizes, die auf den Objekt-Store verweisen, zu löschen. Um nur einige der Datensätze in einem Store zu entfernen, verwenden Sie IDBObjectStore.delete mit einem Schlüssel oder IDBKeyRange.

Syntax

js
clear() 

Parameter

Keine.

Rückgabewert

Ein IDBRequest-Objekt, auf dem nachfolgende Ereignisse, die mit dieser Operation zusammenhängen, ausgelöst werden.

Wenn die Operation erfolgreich ist, ist der Wert der result-Eigenschaft der Anfrage undefined.

Ausnahmen

InvalidStateError DOMException

Wird ausgelöst, wenn der Objekt-Store gelöscht wurde.

ReadOnlyError DOMException

Wird ausgelöst, wenn die mit dieser Operation verbundene Transaktion im nur-Lese-Modus ist.

TransactionInactiveError DOMException

Wird ausgelöst, wenn die Transaktion dieses IDBObjectStore inaktiv ist.

Beispiele

Im folgenden Code-Snippet öffnen wir eine Lese-/Schreib-Transaktion auf unserer Datenbank und leeren alle aktuellen Daten des Objekt-Stores mit clear(). Für ein vollständiges funktionierendes Beispiel siehe unsere To-do Notifications App (Beispiel live ansehen).

js
// Let us open our database const DBOpenRequest = window.indexedDB.open("toDoList", 4); DBOpenRequest.onsuccess = (event) => { note.appendChild(document.createElement("li")).textContent = "Database initialized."; // store the result of opening the database in the db variable. // This is used a lot below db = DBOpenRequest.result; // Clear all the data from the object store clearData(); }; function clearData() { // open a read/write db transaction, ready for clearing the data const transaction = db.transaction(["toDoList"], "readwrite"); // report on the success of the transaction completing, when everything is done transaction.oncomplete = (event) => { note.appendChild(document.createElement("li")).textContent = "Transaction completed."; }; transaction.onerror = (event) => { note.appendChild(document.createElement("li")).textContent = `Transaction not opened due to error: ${transaction.error}`; }; // create an object store on the transaction const objectStore = transaction.objectStore("toDoList"); // Make a request to clear all the data out of the object store const objectStoreRequest = objectStore.clear(); objectStoreRequest.onsuccess = (event) => { // report the success of our request note.appendChild(document.createElement("li")).textContent = "Request successful."; }; } 

Spezifikationen

Specification
Indexed Database API 3.0
# ref-for-dom-idbobjectstore-clear③

Browser-Kompatibilität

Siehe auch