HTML Indexeddb Delete

Last Updated:

HTML Indexeddb Delete

In the following example, we will delete a data (where id=0002) in the table ('MyTableName') under the database ('MyDatabase').

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point"></p> <script> var x = document.getElementById("point"); var i = 0 var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB; var open = indexedDB.open("MyDatabase", 1); open.onupgradeneeded = function(){ var db = open.result; var store = db.createObjectStore("MyTableName", {keyPath: "id"}); var index = store.createIndex("NameIndex", ["name.last", "name.first"]); }; open.onsuccess = function(){ // Start a new transaction var db = open.result; var tx = db.transaction("MyTableName", "readwrite"); var store = tx.objectStore("MyTableName"); var index = store.index("NameIndex"); // Add some data store.put({id: 0001, name: {first: "Brenden", last: "Eich"}, age: 58}); store.put({id: 0002, name: {first: "Dennis", last: "Ritchie"}, age: 70}); store.delete(0002); // Query to get all data var getAll = store.getAll(); getAll.onsuccess = function(){ for(i=0; i<getAll.result.length; i++) x.innerHTML += getAll.result[i].id + ". " +getAll.result[i].name.first + " <br>"; } getAll.onsuccess = function(){ for(i=0; i<getAll.result.length; i++) x.innerHTML += getAll.result[i].id + ". " +getAll.result[i].name.first + " <br>"; } // Close the db when the transaction is done tx.oncomplete = function(){ db.close(); }; } </script> </body> </html>

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
indexeddbYesYesYesYesYesYesYesYesYesYesYesYesYes

Share this Page

Meet the Author