13
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?

5 Answers5

15

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.

Original answer here

For updated realm transaction format

realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            realm.deleteAll();
        }
    });
ralphgabb
  • 10,298
  • 3
  • 47
  • 56
2

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);
    }
Shahab Rauf
  • 3,651
  • 29
  • 38
1

Try this solution. This will delete your Realm database.

public static boolean deleteRealm(RealmConfiguration configuration)

This is a function in Realm, from the docs

Rahul Ahuja
  • 812
  • 10
  • 24
  • Tried already all instance should be closed exception is showing and i have closed almost all realm instance in onDestroy() actually i have used realm many places in the app and have tried to close all but still this exceptions is shown –  Apr 27 '17 at 10:19
  • use exception handling for the same. posting code in next answer. – Rahul Ahuja Apr 27 '17 at 10:32
  • In my case, I use this method realm.deleteAll() but it should be the same as above answer. However I open realm onStart and close onStop in activity or fragment and also immediately close in background thread. – vsatkh May 06 '17 at 13:23
1

realm.deleteAll() will do all the magic but

Never use beginTransaction() unless you want to commitTransaction() 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

Zohab Ali
  • 8,426
  • 4
  • 55
  • 63
-1

for your mentioned exception in comments:

try (Realm realm = Realm.getInstance(realmConfig)) {
    realm.beginTransaction();
    //your operations here
    realm.commitTransaction();
} catch (Exception e) {
    realm.cancelTransaction();
}
Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26
Rahul Ahuja
  • 812
  • 10
  • 24