Prevent deletion of a DocType when another DocType has a Link field referencing it
Problem:
Frappe currently prevents deletion of an individual document when that document is referenced by another document through a Link field. However, the same dependency protection does not appear to be applied when deleting the entire DocType definition.
This can result in a DocType being deleted even though other DocTypes still contain fields that reference it.
Example
Consider two DocTypes:
Booking
└── member = Link → MemberAssume there are existing records in both DocTypes.
When an individual Member record is referenced by a Booking, deleting that specific Member record is prevented because the record is linked from another document.
However, if the entire Member DocType is deleted, the Booking.member Link field still depends on the existence of the Member DocType.
The dependency therefore becomes:
Booking.member
|
v
Member
|
X
DocType deletedThe Link field now points to a DocType that no longer exists.
Current Behavior
The deletion of the DocType definition is allowed even when other DocTypes have Link fields referencing it.
For example:
DocType A
field_x = Link → DocType B
↓
Delete DocType B
↓
DocType B is deletedThe dependent Link fields in DocType A remain, resulting in broken references at the DocType/schema level.
Expected Behavior
Before deleting a DocType, Frappe should check whether that DocType is referenced by fields in other DocTypes.
If dependencies exist, the deletion should either:
- Be prevented and report the dependent DocTypes and fields, or
- Display a clear warning requiring explicit confirmation before proceeding.
For example:
Cannot delete DocType "Member".
The following fields depend on this DocType:
- Booking.member
- Subscription.member
- Payment.member
Remove these dependencies before deleting the DocType.Why This Matters
A Link field creates a dependency not only on individual records but also on the existence of the target DocType itself.
Allowing the target DocType to be removed while dependent Link fields remain can lead to:
- Broken Link field definitions
- Invalid schema relationships
- Existing forms/reports becoming unusable
- Runtime errors when accessing dependent fields
- Difficult-to-diagnose application inconsistencies
- Accidental destruction of dependencies in production environments
This is especially important in applications where multiple custom DocTypes depend on one another.
Proposed Solution
Before deleting a DocType definition, Frappe could inspect the metadata of other DocTypes and identify fields such as:
fieldtype = "Link"
options = "<Target DocType>"and potentially other dependency mechanisms such as Dynamic Link, where applicable.
Conceptually:
Delete DocType B
|
v
Find DocTypes containing fields referencing B
|
+---- No dependencies ----> Allow deletion
|
+---- Dependencies found -> Block/warnThe error or warning should ideally include the affected DocTypes and field names so that the user can understand exactly what must be changed before deletion.
Scope
This request is specifically about DocType definition deletion, not deletion of individual documents.
There are two separate dependency checks:
Document-level dependency
Delete Record B-0001
↓
Check whether another document links to B-0001
↓
Prevent deletion if referencedDocType-level dependency
Delete DocType B
↓
Check whether other DocTypes contain fields referencing B
↓
Prevent or warn if dependencies existThe proposed change concerns the second case.
Acceptance Criteria
A possible implementation could satisfy the following:
- Before deleting a DocType, identify other DocTypes that reference it through Link fields.
- Prevent deletion or require explicit confirmation when dependencies exist.
- Clearly display the dependent DocTypes and field names.
- Do not affect normal deletion of a DocType that has no dependencies.
- Preserve existing document-level link validation behavior.
Additional Consideration
It may be useful to treat this as a general DocType dependency validation mechanism rather than limiting the check only to Link fields, so that other metadata dependencies can also be handled consistently in the future.
Conclusion
The goal is to prevent accidental removal of a DocType that is still part of the schema of other DocTypes and thereby improve referential integrity and metadata safety in Frappe.
Source: frappe/frappe