Skip to main content

Delete Multiple Objects

To delete an object from a realm:

  1. Open a write transaction with realm.write() or realm.writeBlocking().

  2. Query the transaction's mutable realm with realm.query(). Specify the object type as a type parameter passed to query(). Filter the set of returned objects by specifying a query.

  3. Delete the set of RealmResults returned by the query with realmResults.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.