Understanding Cascade Deletion in Salesforce
Cascade deletion is the automatic removal of child records when a related parent record is deleted. In Salesforce, cascade deletion is a critical behavior to understand because it affects data integrity, sharing, triggers, and recovery (Recycle Bin) behavior. This article explains how cascade deletion works across different relationship types, platform implications, and best practices for safe implementation.
How Salesforce Relationship Types Affect Cascade Deletion
Salesforce supports two primary relationship types that determine delete behavior:
- Master-Detail Relationship: Cascade delete is built-in. Deleting a master record automatically deletes all its detail (child) records. This relationship enforces ownership and sharing rules via the master.
- Lookup Relationship: Cascade delete is optional depending on the lookup configuration. When creating a lookup field, admins can choose options like Clear the value, Don’t allow deletion, or Delete this record also (cascade). Not all lookup relationships can be configured for cascade delete—standard object lookups or managed package relationships may behave differently.
Soft Delete vs Hard Delete (Recycle Bin)
When cascade deletion occurs via the UI or a delete API call, child records typically follow the same deletion semantics as the parent:
- Soft delete: Most deletes send records to the Recycle Bin (soft delete). Child records deleted by cascade also go to the Recycle Bin and can be restored if needed.
- Hard delete: Using the API (with
hardDelete) permanently deletes records and bypasses the Recycle Bin. Hard-deleting a parent will permanently remove its children as well.
Apex and Cascade Delete
Deleting records in Apex triggers the same cascade behavior. However, there are important considerations:
- Triggers on child objects fire as part of the same transaction when cascade deletes occur. The execution order runs parent delete triggers first, then child delete triggers for cascaded records.
- Be mindful of limits: large cascades may hit DML row limits, trigger limits, or cause long transactions that time out.
- To explicitly delete related child records in Apex (for custom logic), you can query and delete them in code. Example:
// Example: Explicitly deleting child records in Apex
List
if (!children.isEmpty()) {
delete children; // This will run child delete triggers and follow recycle bin rules
}
Considerations and Caveats
When planning for cascade deletes, consider:
- Unintended data loss: Cascading deletes can remove large volumes of related data. Always review dependency trees (master-detail or lookup) before bulk deletions.
- Trigger & automation behavior: Workflows, Process Builder, Flows, and Apex triggers run for cascaded deletions. This can cause additional processing, recursion, or side effects.
- Sharing/ownership: In master-detail, child records inherit sharing and owner from the master. Deleting the master has a single-source-of-truth effect.
- Lookup limitations: Not all lookup relationships support cascade delete in setup—some system-managed relationships may not allow enabling cascade behavior.
- Large data volumes: For massive cascades, consider batching deletes via the Bulk API or performing safe partial deletes to avoid limits.
Best Practices
- Document relationship behavior and the impact of deleting parent records.
- Use sandbox testing for bulk delete scenarios to observe triggers, flows, and automation interactions.
- Implement confirmation steps or Archive patterns (move data to an archive object) before irreversible deletes.
- Monitor the Recycle Bin and use
undelete(Apex/REST) to restore accidentally deleted records when possible. - For complex deletion logic, prefer explicit Apex deletion with proper batching and error handling instead of relying solely on implicit cascade behaviors.
Summary
Cascade deletion in Salesforce is the automatic deletion of related child records when a parent record is removed. It is automatic for master-detail relationships and configurable (or limited) for lookup relationships. Understanding cascade delete behavior helps you preserve data integrity, avoid accidental data loss, and design safe deletion strategies in Apex and declarative automation.








Leave a Reply