Sub topic 11
Why Deletions Fail and How to Fix Them
This chapter assumes you can write a package.xml, you know the difference between destructiveChangesPre.xml and destructiveChangesPost.xml, and you can run sf project deploy start against a sandbox. Here you will learn the most common reasons a destructive deployment fails or does nothing, how to read the failure, and how to fix it.
How a destructive deployment fails
A destructive deployment is a normal deployment with extra delete steps, so it fails in the same ways any deployment does. It can fail while Salesforce is validating the request, while it is applying a delete, or while it is running tests. Most failures fall into five groups:
- The member name does not match any component (a typo, the wrong type name, or a missing
Object.prefix). - Another component still depends on the one you are deleting.
- The delete runs in the wrong phase (pre instead of post, or the reverse).
- A previously deleted component still holds its name in the Recycle Bin.
- The component cannot be deleted at all, because of its state, its package, or your permissions.
A bad name is the easiest to spot, because the deployment may report it as a warning and still succeed.
Incorrect member names
The Metadata API matches the type and member names exactly. If it finds no match, it generally treats the line as a warning, and the rest of the deployment can still succeed. So a deployment that reports success does not prove anything was deleted. Always read the deploy output.
Typical naming mistakes are:
- Writing a field as
Region__cinstead ofAccount.Region__c. - Using the label instead of the API name, for example
Legacy Account Serviceinstead ofLegacyAccountService. - Putting the member under the wrong
<name>value, for example listing a custom field underCustomObject. - Leaving out a folder prefix for folder-based types such as reports, dashboards, documents, and email templates. These use
FolderName/ComponentName. - Using the wildcard
*. It works in a retrieve or deploy manifest but is not supported in destructive changes files. List each component by its exact name.
Here is a destructive changes file with typical mistakes:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>ApexClass</name>
</types>
<types>
<members>OldRegion__c</members>
<name>CustomField</name>
</types>
</Package>The wildcard is not supported here, and the field has no object prefix. This is the corrected version:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>LegacyAccountService</members>
<name>ApexClass</name>
</types>
<types>
<members>Account.OldRegion__c</members>
<name>CustomField</name>
</types>
</Package>If you are unsure of a component's exact name, take it from the org or from your project instead of typing it from memory. The file name under force-app gives the name for most types. For fields, the path objects/Account/fields/OldRegion__c.field-meta.xml gives Account.OldRegion__c.
sf project generate manifest \
--source-dir force-app/main/default/classes/LegacyAccountService.cls \
--name manifest/check.xmlOpen the generated file and copy the type and member names from it. This avoids guessing at names.
The files also have to be wired up correctly. The CLI needs a regular manifest, or source, alongside the destructive file. Pass the destructive file with --pre-destructive-changes or --post-destructive-changes. If you only delete, use the empty package.xml shown earlier in this tutorial, which has just a <version> element.
Dependency failures
A dependency is any other component that uses the one you want to delete. Salesforce refuses to delete a component while something still depends on it, and the failure usually names the blocker.
Common blockers are:
| Component you delete | Typical blockers |
|---|---|
| Custom field | Apex classes and triggers, flows, validation rules, formula fields, workflow rules, page layouts, list views, reports, email templates |
| Custom object | Apex that references its type, lookup or master-detail fields on other objects, flows, tabs, and page layouts |
| Apex class | Other classes or triggers that call it, flows with Apex actions, Visualforce pages, and scheduled jobs |
| Record type | Profile and permission set assignments, flows, and Apex that references it |
Some references are not obvious. Fields appear in profiles and permission sets as field permissions, in layouts as layout items, and in list views as columns. Salesforce normally cleans up some of these when a field is deleted, but code and automation references block the delete.
Fix with the right phase
Whether a dependency blocks you depends on when the delete runs.
- A post destructive file runs after the deploy. If your deployment also updates the component that referenced the doomed one, the reference is already gone when the delete runs. This is the usual fix for "class A calls class B, and I am deleting B."
- A pre destructive file runs before the deploy. The reference is still in the org at that point, so a pre delete of a referenced component fails.
This pair of files shows the pattern. The deployment sends the updated AccountService, which no longer calls LegacyAccountService, and deletes the old class afterward.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>AccountService</members>
<name>ApexClass</name>
</types>
<version>62.0</version>
</Package><?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>LegacyAccountService</members>
<name>ApexClass</name>
</types>
</Package>sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxUse the pre file when something you are about to deploy conflicts with what is already in the org. One example is replacing a component with a different one under the same name.
Fix by removing the blocker first
If the blocking component is not part of this deployment, you have two choices:
- Include an edit to the blocker in the same deployment (post phase), so it stops referencing the target.
- Delete or edit the blocker in an earlier deployment. This is slower, but it is the safer path when the blocker is large or owned by another team.
When several components depend on each other, such as a field used by a validation rule and a flow, it is usually clearer to split the work into stages. You can list all of them in one file, but the order in which Salesforce processes them is not something to rely on. Staged deletions are easier to validate and to roll back.
Reading the failure
The CLI prints a table of component failures when a deployment fails. Each row has the component name, type, and the problem message. Read the problem text first and the component name second. The message usually says what references the component, or that no component of that name was found.
For long deployments, you can look up a job later:
sf project deploy report --job-id 0Af000000000000AAA --target-org my-sandboxReplace the job ID with the one printed when the deployment started. If you ran the deployment with --dry-run, the report tells you what would have failed, and nothing was saved.
Recycle Bin and name conflicts
When Salesforce deletes some components, such as custom objects and custom fields, it does not remove them at once. They go to the Recycle Bin in a soft-deleted state. While a component sits there:
- Its API name stays reserved. If you try to create a field or object with the same name, the deployment fails with a message that the name is already in use, or similar.
- It can still count against org limits, such as the number of custom fields on an object.
- It can be restored from Setup, which is useful if you deleted by mistake.
You have three ways to deal with a name conflict:
- Choose a new API name for the new component. This is the safest option.
- Empty or purge the component from the Recycle Bin in Setup, which makes the deletion permanent.
- Use the Metadata API deploy option
purgeOnDelete, which skips the Recycle Bin and deletes permanently. Salesforce allows it only in sandbox and Developer Edition orgs, not in production. Checksf project deploy start --helpfor the flag your CLI version offers before depending on it.
Permanent deletion cannot be undone from Setup, so do not purge anything you may need back.
Other reasons a delete is rejected
- Permissions. You need permission to modify metadata, such as the Modify Metadata permission or Modify All Data. A user without it can deploy nothing.
- Managed packages. You cannot delete components that belong to an installed managed package. Remove the package, or work with its publisher.
- Test failures in production. A deployment to production that includes Apex changes or deletions may run tests. A deletion can then fail because an existing test class uses the deleted class. Delete or fix that test in the same deployment.
- The component is already gone. Deleting something that is not in the org does not fail. It produces a warning, and the rest of the deployment goes ahead. This helps with reruns, but it can also hide a typo.
- Local source still holds the file. If you delete a component from the org but keep it in your project, the next deployment re-creates it. Delete the source files as well, or use
sf project delete source, which removes the component from both the org and your project.
A short troubleshooting routine
- Read the full deploy output, including warnings, not just the final status.
- Compare each member name with the real name in your project or the org.
- For a dependency error, find the blocker, then decide between a post delete and an edit of the blocker.
- For a name conflict, check the Recycle Bin and decide whether to rename or purge.
- Rerun with
--dry-rununtil it is clean. Then run the real deployment.
Exercise
You deploy a manifest that updates OrderService so it no longer calls LegacyPricing. You also list LegacyPricing in destructiveChangesPre.xml. The deployment fails with a message that another class still references LegacyPricing. What is the likely cause, and how do you fix it?
Show answer
The pre destructive file runs before the deploy, so OrderService in the org still references LegacyPricing when the delete is attempted. Move LegacyPricing to destructiveChangesPost.xml and pass it with --post-destructive-changes. The delete then runs after the updated OrderService is deployed. Validate with --dry-run first.
Exercise
A deployment reports success, but the custom field Account.OldRegion__c is still in the org. The destructive file lists <members>OldRegion__c</members> under CustomField. What went wrong?
Show answer
The member name is missing the object prefix. A custom field must be written as Account.OldRegion__c. The Metadata API found no component named OldRegion__c, reported a warning instead of an error, and the rest of the deployment succeeded. Correct the name, rerun with --dry-run, and read the warnings.