'Flutter delete Hive Database on Update
I have a Flutter app and I am using Hive to store data.
I have deleted some adapters which were used previously. This caused an error and I have to delete the old database.
Now, if I roll out an update, how do I make sure the old Hive database gets deleted when the user updates the app so that it causes no issues.
Solution 1:[1]
Instead of deleting, run a database migration.
Hive.box("myBox", version: 5, migrator: (oldVersion, newVersion, box) async { await box.delete("unusedKey"); await box.put("newKey", 7); });
If you want to delete it anyway,
- Read the app version, package_info may come in handy for that.
- Delete the old db if this app version/build is running for the first time after installing/updating.
Solution 2:[2]
You can use box.clear()
indeed this is an answer you may be expecting my Best Friend.
Solution 3:[3]
Hive.deleteFromDisk() can just wipe it clean.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Pro Co |
Solution 3 | chaky |