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
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. Launching May 21, 2024. Join the waiting list!