Docs Menu

Docs HomeRealm

Delete Multiple Objects - Kotlin SDK

To delete multiple objects from a realm at the same time:

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

  2. 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.

  3. 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.

←  Delete an Object - Kotlin SDKDelete All Objects of a Type - Kotlin SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.