Search code examples
javaandroidrealmrealm-java

Realm delete all except one table


This is Realm Java so I'm doing

realm.beginTransaction();
realm.deleteAll();
realm.commitTransaction();

I have 20 or so Realm classes. I only want to keep one.

Instead of doing

realm.beginTransaction();
  realm.delete(Table1.class);
  realm.delete(Table2.class);
  realm.delete(Table3.class);
  ...
realm.commitTransaction();

Is there a way to delete all tables except one?

"deleteAll.except(Table12.class)" etc?

thanks!


Solution

  • Sure!

    final RealmConfiguration realmConfiguration = realm.getConfiguration();
    r.executeTransaction((realm) -> {
        for(Class<? extends RealmModel> clazz : realmConfiguration.getRealmObjectClasses()) {
            if(clazz != Table12.class) {
                realm.delete(clazz);
            }
        }
    });