Delete Item from IndexedDB

Deletes an item from IndexedDB.

Contributed by @itsbrunodev

javascript
function deleteFromIndexedDB(databaseName, storeName, id) {
  return new Promise((resolve, reject) => {
    const request = indexedDB.open(databaseName);
 
    request.onsuccess = (event) => {
      const db = event.target.result;
      const transaction = db.transaction(storeName, "readwrite");
      const store = transaction.objectStore(storeName);
 
      const deleteRequest = store.delete(id);
 
      deleteRequest.onsuccess = () => resolve(true); // item deleted successfully
      deleteRequest.onerror = () => reject(deleteRequest.error);
    };
 
    request.onerror = () => reject(request.error);
  });
}
javascript
deleteItemFromIndexedDB("myDatabase", "users", 1)
  .then(console.log) // true
  .catch(console.error);
GitHubEdit on GitHub