Delete Realm Objects - Kotlin SDK
On this page
Note
You can only delete objects from a realm within a write transaction.
To delete a realm file, refer to Delete a Realm.
Delete a Single Object
To delete an object from a realm:
Open a write transaction with realm.write() or realm.writeBlocking().
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.Pass the set of RealmResults returned by the query to mutableRealm.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).find().first() // call delete on the results of a query to delete the object permanently delete(frog) }
Delete Multiple Objects
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) }
Delete All Objects of a Type
To delete all objects of a type from a realm:
Open a write transaction with realm.write() or realm.writeBlocking().
Query the transaction's mutable realm for all objects of that type with realm.query(). Specify the object type as a type parameter passed to
query()
.Delete the set of RealmResults returned by the query with mutableRealm.delete().
realm.write { // fetch all frogs from the realm val frogs: RealmResults<Frog> = this.query<Frog>().find() // call delete on the results of a query to delete those objects permanently delete(frogs) }
Delete Items from a RealmSet
RealmSet instances that contain Realm objects
only store references to those objects, so deleting a Realm object from a
realm also deletes that object from any RealmSet
instances that contain
the object.
You can delete one or more items from a RealmSet
at a time:
To remove one item from a
RealmSet
, pass the element you want to delete to set.remove().To remove multiple items from a
RealmSet
, pass the elements you want to delete to set.removeAll().
realm.write { val myFrog = realm.query<Frog>("name == $0", "Kermit").find().first() val snackSet = findLatest(myFrog)!!.favoriteSnacks // Remove the Flies snack from the set val fliesSnack = snackSet.first { it.name == "Flies" } snackSet.remove(fliesSnack) // Remove all snacks from the set val allSnacks = findLatest(myFrog)!!.favoriteSnacks snackSet.removeAll(allSnacks) }
Alternatively, you can use
set.clear()
to clear all items from a RealmSet
:
// Clear all snacks from the set snackSet.clear()
Delete Dictionary Keys/Values
You can delete RealmDictionary entries in a few ways:
Use
remove()
to remove the key and the valueIf the dictionary's value is nullable, you can set the value of the key to
null
to keep the key.Use
clear()
to remove all keys and values
// Find frogs who have forests with favorite ponds val frogs = realm.query<DeleteTest_Frog>().find() val frogsWithFavoritePonds = frogs.query("favoritePondsByForest.@count > 1").find() val thisFrog = frogsWithFavoritePonds.first() // Set an optional value for a key to null if the key exists if (thisFrog.favoritePondsByForest.containsKey("Hundred Acre Wood")) { realm.write { val mutableFrog = findLatest(thisFrog) if (mutableFrog != null) { mutableFrog.favoritePondsByForest["Hundred Acre Wood"] = null } } } // Remove a key and its value realm.write { findLatest(thisFrog)?.favoritePondsByForest?.remove("Lothlorien") }
Delete an Embedded Object
Warning
Realm Uses Cascading Deletes for Embedded Objects
When you delete a Realm object, Realm automatically deletes any embedded objects referenced by that object. If you want the referenced objects to persist after the deletion of the parent object, use a regular Realm object with a to-one relationship instead.
You can delete an EmbeddedRealmObject.parent() directly or through the parent object.
To delete only an embedded object, you can fetch and delete a specific embedded object or clear the parent's reference to the embedded object, which also deletes the embedded object instance.
Deleting the parent object automatically deletes all of its embedded objects.
// Delete an embedded object directly realm.write { val addressToDelete: Address = this.query<Address>("street == '123 Fake St'").find().first() // Delete the embedded object (nullifies the parent property) delete(addressToDelete) } // Delete an embedded object through the parent realm.write { val propertyToClear: Contact = this.query<Contact>("name == 'Nick Riviera'").find().first() // Clear the parent property (deletes the embedded object instance) propertyToClear.address = null } // Delete parent object (deletes all embedded objects) realm.write { val contactToDelete: Contact = this.query<Contact>("name == 'Nick Riviera'").find().first() delete(contactToDelete) }