Hive - flutter: are boxes deleted when app is closed?

Viewed 1614

I need a way to store simple data in my Flutter project, and I'm considering using Hive flutter.

What I don't quite understand, is if the data stored in the boxes is cleared (as in, deleted from the disk) when the user closes the app/website.

The official documentation says:

Before your application exits, you should call Hive.close() to close all open boxes. Don't worry if the app is killed before you close Hive, it doesn't matter.

which is somewhat ambiguous.

So: Is data stored in boxes cleared upon closing the app/website; and if not, how can I manually clear it?

1 Answers

When you close a box, hive saves the data in the box to a local storage file ("long term memory") and frees the cache ("short term memory") to be used for other stuff. You will not lose your app data. Closing the box is also not strictly necessary, Hive will close the boxes itself on exit. Hive's FAQ has more details: https://docs.hivedb.dev/#/more/faq

If you want to clear data from a given Box box = Hive.box("box_name"), you can:

  • Delete a single entry: box.delete("entry_key")
  • Delete all entries: box.deleteAll(box.keys)
  • Delete the whole box: box.deleteFromDisk() or Hive.deleteBoxFromDisk("box_name")
  • Delete all (open) boxes: Hive.deleteFromDisk()

Note that while the behavior is the same for browsers and apps, the way this is implemented is different: https://docs.hivedb.dev/#/more/browser

Related