Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /
CRUD

Delete Data - .NET SDK

Ejemplo

The following code shows how to delete one object from its realm:

realm.Write(() =>
{
// Remove the instance from the realm.
realm.Remove(dog);
// Discard the reference.
dog = null;
});

Ejemplo

The following code demonstrates how to delete a collection from a realm:

realm.Write(() =>
{
// Find dogs younger than 2 years old.
var puppies = realm.All<Dog>().Where(dog => dog.Age < 2);
// Remove the collection from the realm.
realm.RemoveRange(puppies);
});

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

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

Ejemplo

The following code demonstrates how to perform a chaining delete by first deleting all of Ali's dogs, then deleting Ali:

realm.Write(() =>
{
// Remove all of Ali's dogs.
realm.RemoveRange(ali.Dogs);
// Remove Ali.
realm.Remove(ali);
});

Realm permite borrar todas las instancias de un tipo Realm de un realm.

Ejemplo

The following code demonstrates how to delete all Dog instances from a realm:

realm.Write(() =>
{
// Remove all instances of Dog from the realm.
realm.RemoveAll<Dog>();
});

Es posible borrar todos los objetos del realm. Esto no afecta el esquema del realm. Esto es útil para limpiar rápidamente tu realm mientras creas prototipos.

Ejemplo

The following code demonstrates how to delete everything from a realm:

realm.Write(() =>
{
// Remove all objects from the realm.
realm.RemoveAll();
});

Volver

Update

En esta página