← Changelog
NewApps

Say what happens to a record when its parent is deleted

A belongsTo field takes onDelete: cascade deletes the record with its parent, setNull empties the field, and restrict refuses the delete while a record still points at the parent. Until now deleting a parent left its children holding the id of a record that was gone, and every app cleaned up in its own code.

The delete and everything it takes with it are one transaction: a restrict anywhere down the chain refuses all of it. One delete takes at most 5,000 other records; past that it is refused, so delete the children in batches first. Cascaded deletes are made by the platform and don't run the child model's delete action; Logs show one event per child model, and each record can still be restored.

Set it in the schema file or in the schema editor's field settings. A field without onDelete behaves as before.

More in Deleting a parent.