2

I have a function in my app where the user reset the account whenever he/she desires to do so and I want to delete the whole realm local database and start over with creating a new one after completed process of deleting it.

This is not for migration but rather wipe out the whole database and start over as if the app was deleted and installed again.

Is there any way to do this, I can't seem to find information on this

My code is written in React Native so this is RealmJS

General Grievance
  • 4,555
  • 31
  • 31
  • 45
tompa
  • 21
  • 2

1 Answers1

0

I don't know if this can help you : https://stackoverflow.com/a/26244843/9231356

let realm = try! Realm()
try! realm.write {
    realm.deleteAll()
}
Dannick Bedard
  • 373
  • 3
  • 14
  • 1
    Good answer. Just a caveat that this code will not work if Realm has been 'talked to' before this code. e.g. as soon as you app does *anything* with Realm, it remembers that connection. Even if you delete the files, it will re-create itself. So, this code needs to run before any connection to realm is made. – Jay Feb 10 '21 at 19:38
  • So you are saying that this could work if I create a code where the user "Delete Account", the app restarts but this time I run the code above before anything else ? – tompa Feb 10 '21 at 19:59
  • I forgot to mention that I use an encryption key for the database so I somehow need to delete it while the key (or realm instance for corresponding key) is in memory.. – tompa Feb 10 '21 at 20:59
  • @tompa yes. As long as that code runs *before* making any other connection to Realm it will work. Otherwise the data will re-populate. – Jay Feb 11 '21 at 17:13