Ecto.Model.Dependent

Defines callbacks for handling dependencies (associations).

Such callbacks are typically declared via the has_many/3 macro in your model’s schema block. For example:

has_many :comments, MyApp.Comment, on_delete: :fetch_and_delete

`:on_delete` options

There are four different behaviors you can set for your associations when the parent is deleted:

Keep in mind these options are only available for has_many/3 macros.

Alternatives

Ecto also provides an :on_delete option when using references/2 in migrations. This allows you to set what to perform when an entry is deleted in your schema and effectively, at the database level. When you want to push as much responsibilty down to the schema, that approach would better serve you.

However, using the :on_delete option in has_many/3 would afford you more flexibility. It does not require you to run migrations every time you want to change the behavior and it is not database specific.

Source