Sub topic 11
Why deletions fail and how to fix them
This chapter assumes you can write a package.xml, know the difference between destructiveChangesPre.xml and destructiveChangesPost.xml, and can run sf project deploy start against a sandbox. It covers the failures you will see most often and a way to work through them.
Two kinds of failure
A destructive deployment can go wrong in two ways.
- Loud failure. The deployment fails and the org keeps every component. The error names the component that blocked the deletion.
- Quiet non-deletion. The deployment succeeds but the component is still there. This usually means the member name did not match anything.
Dependencies block the delete
Salesforce refuses to delete a component that another component still uses. Common examples:
- A custom field referenced by an Apex class, a flow, a validation rule, a formula field, a page layout, or a report.
- An Apex class called by another class, a trigger, or an active flow.
- A custom object that still has fields, records, or relationships pointing at it.
The error message names the dependent component. Typical wording is "Cannot delete custom field ... referenced by ...". Read the message from the bottom up: the last component named is the one you must change or remove first.
There are two ways to fix a dependency problem:
- Remove the reference by editing the dependent component and deploying that change.
- Delete the dependent component in the same deployment.
Choose pre or post to fix ordering
Many dependency failures come from the order of operations, not from a missing edit. Suppose AccountService calls LegacyHelper, and you want to delete LegacyHelper. You also have an updated AccountService that no longer calls it.
If you delete first, the old AccountService still references the class and the delete fails. Use a post file so the updated class deploys first:
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxThe manifest must include the updated AccountService. If it does not, nothing removes the reference and the delete still fails.
Use a pre file in the opposite case. If the incoming deployment would conflict with an existing component, for example by reusing a name, delete the old one before the deploy step.
Names that match nothing
If a deployment succeeds but the component remains, check the destructive file. Salesforce does not treat a missing component as an error. It reports a warning and continues, so a typo is easy to miss.
Check these points:
- A field member is written
Object.FieldName, for exampleAccount.OldRegion__c, and includes the__csuffix. - The
<name>value is the exact metadata type, such asCustomFieldorApexClass. - The member is spelled out. The wildcard
*is not supported in destructive changes files. - The
<version>element is optional in a destructive file, but it must not conflict with the manifest.
This file is correct:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Account.OldRegion__c</members>
<name>CustomField</name>
</types>
</Package>A deletion-only deployment still needs a package.xml. Use an empty one that contains only <version>.
Read the full result
The terminal summary can hide warnings. Use the JSON output or the report command to see every message:
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandbox \
--jsonAdd --dry-run while you debug. It runs the same checks without saving changes, so you can retry as often as needed.
Fields are not removed instantly
Even when a field delete succeeds, Salesforce keeps the field in a deleted state for a period, and it can be restored. The name stays reserved during that time. If you try to create a new field with the same API name right away, you may get a duplicate-name error. Restore the old field, or purge it as described in the Recycle Bin chapter, before reusing the name.
Exercise
A post destructive deployment to delete Account.OldRegion__c fails with an error that a validation rule references the field. You have an updated validation rule file in your project. What do you change, and why does the file choice matter?
Show answer
Add the updated validation rule to package.xml so it deploys in the same run, and keep the field in destructiveChangesPost.xml. The post file deletes after the deploy step, so the new rule no longer references the field when the delete runs. A pre file would delete first, while the old rule still references the field, and the deployment would fail.