Docs Menu

Delete an Object - Kotlin SDK

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 for the object 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. To ensure your query returns the correct object, filter with unique identifying information such as a primary key value.
  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?.also { delete(it) }

You can only delete objects from a realm within a write transaction.

←  Delete - Kotlin SDKDelete Multiple Objects - Kotlin SDK →
Give Feedback
© 2022 MongoDB, Inc.


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