Realm.deleteRealmFile() doesn't return the correct result
I have a user session based app. On logout I call Realm.deleteRealmFile(context) and it returns true. But when I log in as another user, the data of the previous Realm still exists. How to remove all data ?
I'm on Android and using Realm 0.76 for Android.
Hi @ImGauravC If you call deleteRealmFile() while the Realm is still open the file will not be deleted, ie. you need to call Realm.close() on all your open instances.
Alternatively instead of deleting the Realm file you can call Realm.clear(Session.class) which will delete all objects of type Session while keeping the Realm file.
Does that answer your question?
Thanks. That worked. But deleteRealmFile does return true. Shouldn't it return false if the file is not deleted in such a case?
Hi @ImGauravC
We rely on the return value of Java's File.delete() which returns true and it reports the wrong result due to how the file system works.
It is documented in the JavaDoc for this reason as well, but we could probably add some additional checks that could catch some more edge cases. However for some use cases it will probably never be possible, like eg. two processes having the file open.
I see. Thanks for the response @cmelchior :)
We have added a check to Realm.deleteRealmFile() that will throw an error if called incorrectly. https://github.com/realm/realm-java/pull/787
It will be released as part of 0.79.0.