CRUD - Delete - React Native SDK
The examples on this page use the following schema:
Delete an Object
To delete an object from a realm, pass the object to Realm.delete() inside of a write transaction.
In the following example of a DogList
component, we:
Get access to the opened realm instance by calling the
useRealm()
hook.Retrieve all dogs in the realm instance by passing
Dog
to theuseQuery()
hook.Create a component method
deleteDog()
that takes in aDog
object as a parameter. Within the method, we passRealm.delete()
theDog
object, deleting it from the realm.Map through the dogs to render a list of
Text
components that contain a dog'sname
and a "Delete Dog" button.Add an onPress event on the "Delete Dog" button that calls the component's
deleteDog()
method.
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 throws an error.
Delete Multiple Objects
You can delete multiple objects from a realm in a couple of ways:
To delete all objects of a given object type from a realm, pass the results of useQuery(<ObjectType>) to the Realm.delete() method inside of a write transaction.
To delete many specific objects from a realm, pass Collection.filtered() to
Realm.delete()
inside of a write transaction.
In the following example of a DogList
component, we:
Retrieve the realm instance using the
useRealm()
hook.Set a variable
myDogs
to all theDog
objects by passing theDog
class to theuseQuery()
hook.Create a component method
deleteAllYoungDogObjects()
that performs a write transaction. Within the write transaction, we set a variable,youngDogs
, to the result ofmyDogs.filtered()
with a query to obtain all dogs younger than three. Then passyoungDogs
torealm.delete()
, deleting all young dogs from the realm.Create a component method
deleteAllDogObjects()
that performs a write transaction. Within the write transaction, we passmyDogs
torealm.delete()
, deleting all the dogs from the realm.Map through the dogs to render a list of
Text
components that contain a dog'sname
andage
.Add an
onPress
event on the "Delete Young Dog Objects" button that callsdeleteAllYoungDogObjects()
, deleting all young dogs from the realm, which triggers a re-render and removes them from the UI.Add an
onPress
event on the "Delete All Dog Objects" button that callsdeleteAllDogObjects()
, deleting every dog from the realm, which triggers a re-render and removes them from the UI.
Note
When you delete objects from the realm instance, the component automatically re-renders and removes them from the UI.
Delete All Objects in a Realm
To delete all objects from the realm, call Realm.deleteAll() inside of a write transaction. This clears the realm of all object instances but does not affect the realm's schema.
In the following example of a DeleteProfileSettingsScreen
component, we:
Get access to the opened realm instance by calling the
useRealm()
hook within the component.Create a component method
deleteAllData()
that performs a write transaction and callsRealm.deleteAll()
, deleting all objects from the realm.Add an
onPress
event on the "Delete all data" button that callsdeleteAllData()
.
Tip
Delete All In Development
Realm.deleteAll() is a useful method to quickly clear out your realm in the course of development. For example, rather than writing a migration to update objects to a new schema, it may be faster to delete and then re-generate the objects with the app itself.