IndexedDB: Deleting data

Deleting the database, an object store and data

Delete an entire IndexedDB database

//first import deleteDb from `idb`

import { deleteDB } from 'idb'
//or 
import { deleteDB } from 'https://cdn.jsdelivr.net/npm/idb@7/+esm'

//then:

const dbName = 'mydbname'
await deleteDB(dbName)

To delete data in an object store

We use a transaction:

(async () => {
  //...

  const dbName = 'mydbname'
  const storeName = 'store1'
  const version = 1

  const db = await openDB(dbName, version, {
    upgrade(db, oldVersion, newVersion, transaction) {
      const store = db.createObjectStore(storeName)
    }
  })

  const tx = await db.transaction(storeName, 'readwrite')
  const store = await tx.objectStore(storeName)

  const key = 'Hello again'
  await store.delete(key)
  await tx.done
})()

Lessons in this unit:

0: Introduction
1: Loading the idb library
2: Creating a database and a store
3: Adding data into a store
4: Retriving data from a store
5: ▶︎ Deleting data
6: Migrations
7: Unique keys
Want to learn more? Check out our courses