Dinu Dinu - 4 months ago 59
Android Question

How to truncate all tables in realm android

Hi im trying to truncateall tables in android when a user logs out. im using realms default path only.

realm = Realm.getInstance(getApplicationContext());

public void clearDB() {
Realm.deleteRealmFile(instance);
}

Answer

Update

Use realm.delete(Foo.class) instead as clear() is deprecated. From 0.91.0 all @Deprecated methods will be removed.


Christian from Realm here. That approach will work as long as you have closed all open Realm instances. Another approach is clearing the tables you want like this:

realm = Realm.getInstance(getApplicationContext());

public void clearDB() {
  realm.executeTransaction(new Realm.Transaction() {
     @Override
     public void execute(Realm realm) {
        realm.clear(Foo.class);
        realm.clear(Bar.class);
     }
  });
}

You can read more here: http://realm.io/docs/java/0.77.0/api/io/realm/Realm.html#clear(java.lang.Class)

Comments