Delete Multiple Objects - Kotlin SDK
To delete multiple objects from a realm at the same time:
Open a write transaction with realm.write() or realm.writeBlocking().
Query the transaction's mutable realm for the objects you want to delete with realm.query(). Specify the object type as a type parameter passed to
query()
. Filter the set of returned objects by specifying a query.Delete the set of RealmResults returned by the query with realmResults.delete().
realm.write { // fetch 7 frogs of the bullfrog species from the realm val frogs: RealmResults<Frog> = this.query<Frog>("species == 'bullfrog' LIMIT(7)").find() // call delete on the results of a query to delete those objects permanently delete(frogs) }
Note
You can only delete objects from a realm within a write transaction.