I'm using Sembast in a Flutter app. I have to refresh all the data in a store and need a way to removeall documents in a store. I looked at the API and only way I can see is either to get all keys and use them in a finder to delete them or drop the store and recreate it. Not sure if I overlooked something to support this?


Solution 1: Kyle Petryszak

You can use the sembast deleteAll method to delete all records in a Database, which returns the list of keys that have been deleted that you can use in whatever code you need to recreate the store.

If you do not need to recreate the store you can use the sembast clear method to delete all records in the store.


Solution 2: alextk

The delete https://pub.dev/documentation/sembast/latest/sembast/StoreRef/delete.html method can (and should) be used. Maybe there should be a clear method that simply calls delete without filtering, it would be similar to the indexed db API...

Some info here: https://github.com/tekartik/sembast.dart/blob/master/sembast/doc/writes.md#delete-records


Solution 3: Elia Weiss

I did this:

var receiptStore = intMapStoreFactory.store("receipts");
final database = await databaseFactoryIo.openDatabase(dbPath, version: 2,
    onVersionChanged: (Database db, int oldVersion, int newVersion) async {
  if (oldVersion == 1) {
    await receiptStore.delete(db);
  }
});

so that I 'upgrade' the DB by deleting it.


Solution 4: Aditya Joardar

This works for me -:

db.close();
await databaseFactoryIo.deleteDatabase(_database.path);
db = null;