Deleting multiple records in IndexedDB based on index

I deleted multiple records belonging to an index by this way using idb :

    var tx = idb.transaction("MyObjectStore", 'readwrite');
    var index = tx.store.index('my_relid_idx');
    var pdestroy = index.openCursor(RelID);
    pdestroy.then(async cursor => {
        while (cursor) {
            cursor.delete();
            cursor = await cursor.continue();
        }
    })

I found out an easiest approach with this

index.iterateCursor(IDBKeyRange, (cursor) => {
  if(cursor){
    cursor.delete();
    cursor.continue();
  }
});

that way if you have it under an async function you can just use the

await index.iterateCursor...

and wait for the promise on the other side


You have to get primary keys to delete the records.

var pdestroy = tagIndex.openKeyCursor(IDBKeyRange.only(tagno)); 
pdestroy.onsuccess = function() {
  var cursor = pdestroy.result;
  if (cursor) {
      pstore.delete(cursor.primaryKey);
      cursor.continue();
  }
}

Alternatively, but not efficient

var pdestroy = tagIndex.openCursor(IDBKeyRange.only(tagno)); 
pdestroy.onsuccess = function() {
  var cursor = pdestroy.result;
  if (cursor) {
      cursor.delete();
      cursor.continue();
  }
}