Praveen Kumar U
Praveen Kumar U

Reputation: 297

How to delete all the boxes in Hive Flutter?

I am developing an application using Flutter; it will store some data locally, so I decided to use Hive package which was really amazing package to store data. So now I will store all the data locally when the user press the sync button. After that, if the user clicks sync again, I have to delete all the boxes and store data which may or may not have the same box name.
I don't want to increase the application storage to increase if the sync button is clicked, I want to delete all the boxes and again I want to create the box.

Upvotes: 11

Views: 30047

Answers (3)

Vinoth
Vinoth

Reputation: 9734

You can use deleteFromDisk method. It removes the file which contains the box and closes the box.

_myCourseBox.deleteFromDisk();

Upvotes: 25

Vishnar Tadeleratha
Vishnar Tadeleratha

Reputation: 361

Unfortunately, I don't think a feature to clear() all (opened, plus unopened) Hive boxes has been implemented. The box files are basically thrown into your device's application document directory as *.hive files (with compacted files as *.hivec and lock files as *.lock). There's no separate key-value store (or Hive box) that keeps track of previously created boxes, though you can implement such a Hive box yourself and iterate over those values as you please.

In your case, where you simply want to delete all the boxes in one sweep, a workaround could be to place all Hive boxes into a sub-directory (using Hive.initFlutter('chosenPath') ) and simply delete the directory when necessary using standard file operations. The only gotcha being that you call Hive.close() to close all open boxes before attempting this (to delete the undeletable *.lock files).

To simplify cross-platform references to the app's document directory you can use the path_provider package. Add path_provider: ^1.6.5 to your dependencies in pubspec.yaml, and where necessary in your dart application import 'package:path_provider/path_provider.dart'; and import 'dart:io'; for file operations;

Let's say you use Hive.initFlutter('chosenPath') to initialise and store your Hive.

So whenever you want to clear all boxes (after ensuring Hive.close() has been called), you could use the following code:

// Get the application's document directory    
var appDir = await getApplicationDocumentsDirectory(); 

// Get the chosen sub-directory for Hive files
var hiveDb = Directory('${appDir.path}/chosenPath');

// Delete the Hive directory and all its files
hiveDb.delete(recursive: true);

The directory will be re-generated from scratch the next time you call Hive.initFlutter('chosenPath').

Upvotes: 13

You didn't share any code so I will just give an example.

I would suggest you to open the boxes in your main function

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  Hive.registerAdapter(yourAdapter());
  await Hive.openBox('yourBoxName');
}

When user wants to sync, you can do following;

// It will delete all the entry in the box
Hive.box('yourBoxName').clear();
yourSyncOperation();

Upvotes: 6

Related Questions