You are here: Basics Operations & Concepts > Delete Behavior > Cascading Deletion

Cascading Deletion

By default db4o only deletes objects which are passed to the delete-method and doesn't delete referenced objects. You can easily change that. Configure the cascading deletion behavior in the configuration for certain classes or certain fields.

For example we mark that the object in the 'pilot'-field is also deleted:

IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
config.Common.ObjectClass(typeof (Car)).ObjectField("pilot").CascadeOnDelete(true);
using (IObjectContainer container = Db4oEmbedded.OpenFile(config, DatabaseFile))
{
DeletionExamples.cs: Mark field for cascading deletion
Dim config As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
config.Common.ObjectClass(GetType(Car)).ObjectField("pilot").CascadeOnDelete(True)
Using container As IObjectContainer = Db4oEmbedded.OpenFile(config, DatabaseFile)
DeletionExamples.vb: Mark field for cascading deletion

When we now delete the car, the pilot of that car is also deleted.

Car car = FindCar(container);
container.Delete(car);
// Now the pilot is also gone
AssertEquals(0, AllPilots(container).Count);
DeletionExamples.cs: Cascade deletion
Dim car As Car = FindCar(container)
container.Delete(car)
' Now the pilot is also gone
AssertEquals(0, AllPilots(container).Count)
DeletionExamples.vb: Cascade deletion