Reputation:
mRealm.beginTransaction();
mRealm.clear(AboutItemRealm.class);
mRealm.clear(AgendaItemRealm.class);
mRealm.clear(AttendeesItemRealm.class);
mRealm.clear(DocumentsItemRealm.class);
mRealm.clear(FAQsItemRealm.class);
mRealm.clear(GalleryItemRealm.class);
mRealm.clear(GoodToKnowItemRealm.class);
mRealm.clear(MultiEventItemRealm.class);
mRealm.clear(ReservationItemRealm.class);
mRealm.clear(SingleEventItemRealm.class);
mRealm.clear(SpeakerItemRealm.class);
mRealm.commitTransaction();
mRealm.close();
When i logout from app i need to clear data of realm for that i have to clear every class like this so is there any way to delete all data of realm without having to write all this mRealm.clear(ClassName.class) for every structure?
Upvotes: 13
Views: 15267
Reputation: 10528
The right way of deleting your entire Realm (schema) is to use :
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
// delete all realm objects
realm.deleteAll();
//commit realm changes
realm.commitTransaction();
Please be aware, that this will delete all realm object that extends RealmObject class.
For updated realm transaction format
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.deleteAll();
}
});
Upvotes: 15
Reputation: 9544
realm.deleteAll()
will do all the magic but
Never use
beginTransaction()
unless you want tocommitTransaction()
transaction manually for some reason
Best way is to use executeTransaction()
because it will commitTransaction
even though if any exception raised inside transaction
try {
realm.executeTransaction { realm ->
realm.deleteAll()
}
} finally {
realm?.close()
}
If you really want to use Realm in an efficient and easy way then read This Article. I personally found it alot useful than official documentation
Upvotes: 1
Reputation: 3931
First close all realm instances and then call deleteRealm
public static void removeAllData(Realm realm)
{
try {
realm.close();
Realm.deleteRealm(realm.getConfiguration());
} catch (Exception e) {
Log.e(TAG, "removeAllData:" + e.getMessage());
}
}
Realm.Java
/**
* Deletes the Realm file specified by the given {@link RealmConfiguration} from the filesystem.
* All Realm instances must be closed before calling this method.
*
* @param configuration a {@link RealmConfiguration}.
* @return {@code false} if a file could not be deleted. The failing file will be logged.
* @throws IllegalStateException if not all realm instances are closed.
*/
public static boolean deleteRealm(RealmConfiguration configuration) {
return BaseRealm.deleteRealm(configuration);
}
Upvotes: 2
Reputation: 812
for your mentioned exception in comments:
try (Realm realm = Realm.getInstance(realmConfig)) {
realm.beginTransaction();
//your operations here
realm.commitTransaction();
} catch (Exception e) {
realm.cancelTransaction();
}
Upvotes: -1
Reputation: 812
Try this solution. This will delete your Realm database.
public static boolean deleteRealm(RealmConfiguration configuration)
This is a function in Realm, from the docs
Upvotes: 1