Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Realm default database clear

Tags:

android

realm

How to clear default realm database in android ? I tried following code but cannot resolve method deleteRealmFile.

Method 1 :

try {
   Realm.deleteRealmFile(context);
  //Realm file has been deleted.

} catch (Exception ex){
   ex.printStackTrace();
  //No Realm file to remove.
}

I tried to delete using configuration.

Method 2 :

try {
     Realm.deleteRealm(realm.getConfiguration());
     //Realm file has been deleted.
} catch (Exception ex){
     ex.printStackTrace();
     //No Realm file to remove.
}

but is gives the error:

java.lang.IllegalStateException: It's not allowed to delete the file associated with an open Realm. Remember to close() all the instances of the Realm before deleting its file.
like image 540
Harshal Bhatt Avatar asked Dec 24 '15 06:12

Harshal Bhatt


People also ask

How do you delete a realm database?

try { Realm. deleteRealmFile(getActivity()); //Realm file has been deleted. } catch (Exception ex){ ex. printStackTrace(); //No Realm file to remove. }

Where is realm database stored Android?

You can find your realm file in the Device File Explorer tab at the bottom right of Android Studio. The location will be /data/data/com.

What database does realm use?

The Realm SDKs use Atlas Device Sync to synchronize app data between clients and MongoDB Atlas.

What is realm database Android?

Realm is an object database that is simple to embed in your mobile app. Realm is a developer-friendly alternative to mobile databases such as SQLite and CoreData. Before we start, create an Android application.


1 Answers

Before Realm.deleteRealm(realm.getConfiguration()); just add realm.close() like below and it works like a charm

try {
      realm.close()
      Realm.deleteRealm(realm.getConfiguration());
                //Realm file has been deleted.
} catch (Exception ex){
                ex.printStackTrace();
                //No Realm file to remove.
}
like image 97
Vishwajit Palankar Avatar answered Sep 18 '22 10:09

Vishwajit Palankar