Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete All Realm Objects During Runtime

Tags:

java

realm

on iOS we can easily call realm.deleteAllObjects(); to remove all objects in our Realm database.

How do we achieve the same in Android?

like image 935
JayVDiyk Avatar asked Dec 18 '15 03:12

JayVDiyk


People also ask

How do I delete all data from a realm?

The right way of deleting your entire Realm (schema) is to use : Realm realm = Realm. getDefaultInstance(); realm. beginTransaction(); // delete all realm objects realm.

How do you delete a realm object?

To delete all objects from the realm, call Realm. deleteAll() inside of a write transaction. This clears the realm of all object instances but does not affect the realm's schema.


1 Answers

You can do this by using results- For instance, if I want to delete all Dog objects, I can do the following-

// obtain the results of a query

RealmResults<Dog> results = realm.where(Dog.class).findAll();

// All changes to data must happen in a transaction
realm.beginTransaction();

// Delete all matches
results.deleteAll();

realm.commitTransaction();

Ref: documentation

like image 186
Shripada Avatar answered Sep 21 '22 15:09

Shripada