48

I want remove all message object from realm those are equal to userid

RealmQuery<Message> rowQuery = realm.where(Message.class).equalTo(Message.USER_ID, userId);
realm.beginTransaction();
//TODO : here I want to remove all messages where userId is equal to "9789273498708475"
realm.commitTransaction();
fawaad
  • 341
  • 6
  • 12
Pradeep Bishnoi
  • 1,843
  • 2
  • 22
  • 26

5 Answers5

123

In 0.88.3 and below you can do:

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        RealmResults<Message> rows = realm.where(Message.class).equalTo(Message.USER_ID,userId).findAll();
        rows.clear();
    }
});

From 0.89 (next release) this will be deleteAllFromRealm() instead.

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        RealmResults<Message> result = realm.where(Message.class).equalTo(Message.USER_ID,userId).findAll();
        result.deleteAllFromRealm();
    }
});
Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
  • 7
    Just fyi it would be nice if there was a comment in the deprecated `.clear()` method that `.deleteAllFromRealm()` is now the correct method to use. – Ed Lee Dec 08 '16 at 23:21
  • 2
    @EddieLee it's in the [0.89.0 change log](https://github.com/realm/realm-java/blob/master/CHANGELOG.md#0890) – EpicPandaForce Apr 03 '17 at 15:35
  • Imagine we have another model which has `RealmList`. If somehow we delete whole `Message` rows, does it delete on that Model too? (Please take a look at https://stackoverflow.com/questions/49029813/realm-the-size-of-filled-realmlist-in-realmobject-is-zero) – Dr.jacky Mar 03 '18 at 07:07
  • realm.executeTransaction { var result = realm.where(JsonValidate::class.java).equalTo("Transaction.Number",No).findAll(); result.deleteAllFromRealm() } I am using this query to delete, delete working fine but, I cant add again the same number it says "already existed" How to resolve it. Note: I dont want insertOrUodate method – Mani Feb 01 '21 at 07:23
11

This must be done between the realm.beginTransaction(); and the realm.commitTransaction(); I also listed in the code example a few args().

realm.beginTransaction();
MessageObject messageobj = realm.where(Message.class)
                         .findFirst()  //or
                         .greaterThan("age", 10) // implicit AND
                         .beginGroup() //or you can use
                         .equalTo("name", "Peter")
                         .or()
                         .contains("name", "Jo")
                         .endGroup()
                         .findAll();
messageobj.deleteFromRealm();
realm.commitTransaction();
kit
  • 1,166
  • 5
  • 16
  • 23
Roger Belk
  • 309
  • 5
  • 17
  • 1
    `removeFromRealm()` must be done in write transaction (between `.beginTransaction()` & `.commitTransaction()`) – muazhud Mar 13 '17 at 08:00
  • While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit](http://stackoverflow.com/posts/41421851/edit) your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others. – Dev-iL Apr 04 '17 at 12:36
4

this is how i Used it

  RealmResults<CartDBItems> rows= realm.where(CartDBItems.class).equalTo("id", id).findAll();;
         rows.deleteAllFromRealm();
other Tall guy
  • 373
  • 2
  • 8
2

For below v10 realm android users. Given below will work fine

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        RealmResults<Note> result = realm.where(Notes.class).equalTo(Note.NOTE_ID,noteID).findAll();
        result.deleteAllFromRealm();
    }
});

However, it will crash for v10 and above

Running transactions on the UI thread has been disabled.

Your app will not crash if you run execute transactions from the UI thread as long as you are running Realm prior to v10. For the upcoming v10 release we are introducing two new settings in RealmConfiguration.Builder, namely allowWritesOnUiThread and allowQueriesOnUiThread, which will allow users to control whether it is allowed to run transactions and/or queries from the UI thread.

RealmConfiguration config = new RealmConfiguration.Builder()
  .allowWritesOnUiThread(true)
  .build()

And it will work like it always has. So you can decide when/if you want to opt into the new behavior.

Aman Srivastava
  • 1,007
  • 1
  • 13
  • 25
1
 myRealm.beginTransaction();

     RealmResults<Datos> datos = myRealm.where(DatosCliente.class)
                        .equalTo("folio",FOLIO)
                        .findAll();

 datos.deleteAllFromRealm();
  • Can you please elaborate your answer and explain what this code does? This would be helpful for future visitors. – Uwe Plonus Feb 27 '18 at 21:11