Delete Multiple Objects
To delete an object from a realm:
Open a write transaction with
realm.write()orrealm.writeBlocking().Query the transaction's mutable realm with
realm.query(). Specify the object type as a type parameter passed toquery(). Filter the set of returned objects by specifying a query.Delete the set of
RealmResultsreturned by the query withrealmResults.delete().
realm.write {
// fetch the frog by primary key value, passed in as argument number 0
val frog: Frog? =
this.query<Frog>("_id == $0", PRIMARY_KEY_VALUE).first().find()
// call delete on the results of a query to delete the object permanently
frog?.delete()
}
note
You can only delete objects in a realm within a write transaction.