Docs Menu

Docs HomeRealm

CRUD - Delete - Swift SDK

On this page

  • Delete Realm Objects
  • About The Examples On This Page
  • Delete an Object
  • Delete Multiple Objects
  • Delete an Object and Its Related Objects
  • Delete All Objects of a Specific Type
  • Delete All Objects in a Realm
  • Delete Map Keys/Values
  • Delete MutableSet Elements
  • Delete the Value of an AnyRealmValue

Deleting Realm Objects must occur within write transactions. For more information about write trasactions, see: Key Concept: Transactions.

If you want to delete the Realm file itself, see: Delete a Realm.

Important

Do not use objects after delete

You cannot access or modify an object after you have deleted it from a realm. If you try to use a deleted object, Realm Database throws an error.

The examples on this page use the following models:

Sometimes, you want to delete related objects when you delete the parent object. We call this a chaining delete. Realm Database does not delete the related objects for you. If you do not delete the objects yourself, they remain orphaned in your realm. Whether or not this is a problem depends on your application's needs.

The best way to delete dependent objects is to iterate through the dependencies and delete them before deleting the parent object.

You can delete map entries in a few ways:

  • Use removeObject(for:) to remove the key and the value

  • If the dictionary's value is optional, you can set the value of the key to nil to keep the key.

let realm = try! Realm()
// Find the dog we want to update
let wolfie = realm.objects(Dog.self).where {
$0.name == "Wolfie"
}.first!
// Delete an entry
try! realm.write {
// Use removeObject(for:)
wolfie.favoriteParksByCity.removeObject(for: "New York")
// Or assign `nil` to delete non-optional values.
// If the value type were optional (e.g. Map<String, String?>)
// this would assign `nil` to that entry rather than deleting it.
wolfie.favoriteParksByCity["New York"] = nil
}
XCTAssertNil(wolfie.favoriteParksByCity["New York"])

You can delete specific elements from a MutableSet, or clear all of the elements from the set. If you are working with multiple sets, you can also remove elements in one set from the other set; see: Update a MutableSet Property.

let realm = try! Realm()
// Record a dog's name and list of cities he has visited.
let dog = Dog()
dog.name = "Maui"
let dogCitiesVisited = ["New York", "Boston", "Toronto"]
try! realm.write {
realm.add(dog)
dog.citiesVisited.insert(objectsIn: dogCitiesVisited)
}
XCTAssertEqual(dog.citiesVisited.count, 3)
// Later... we decide the dog didn't really visit Toronto
// since the plane just stopped there for a layover.
// Remove the element from the set.
try! realm.write {
dog.citiesVisited.remove("Toronto")
}
XCTAssertEqual(dog.citiesVisited.count, 2)
// Or, in the case where the person entered the data for
// the wrong dog, remove all elements from the set.
try! realm.write {
dog.citiesVisited.removeAll()
}
XCTAssertEqual(dog.citiesVisited.count, 0)

To delete the value of an AnyRealmValue, set it to .none.

let realm = try! Realm()
// Wolfie's companion is "Fluffy the Cat", represented by a string.
// Fluffy has gone to visit friends for the summer, so Wolfie has no companion.
let wolfie = realm.objects(Dog.self).where {
$0.name == "Wolfie"
}.first!
try! realm.write {
// You cannot set an AnyRealmValue to nil; you must set it to `.none`, instead.
wolfie.companion = .none
}
←  CRUD - Update - Swift SDKThreading - Swift SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

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