Sub topic 8
Deleting Added Components with Destructive Changes
Redeploying the previous version of your metadata restores components that the bad release changed. It does not remove components the bad release added. A new Apex class, custom field, or flow stays in the org until you delete it. This chapter covers how to delete those components with a destructive changes manifest.
This chapter assumes you can read and write a package.xml. It also assumes you have used sf project deploy start, and that you have a Git history with a merge commit for each release. The previous chapter covered redeploying the old versions from source control. Here you add the deletion step.
Why a redeploy leaves added components behind
A deployment is additive. It creates or updates the components in your manifest and does nothing to components that are absent from it. Suppose release 42 changed AccountService and added OrderHelper. If you run git revert on the release merge and redeploy, AccountService returns to its old version. OrderHelper no longer exists in your source, but it still exists in the org.
Leftover components cause three kinds of trouble:
- An orphaned Apex class or trigger still runs and still counts toward test coverage calculations.
- A leftover custom field keeps showing on page layouts and in reports.
- Your source and your org no longer match, so the next deployment starts from a false baseline.
Destructive changes close that gap. They tell the Metadata API to delete named components as part of a deployment.
The destructive changes manifest
A destructive changes file looks like a package.xml. It lists component types and member names, and the platform deletes every member it lists. This example deletes the Apex class OrderHelper:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>OrderHelper</members>
<name>ApexClass</name>
</types>
</Package>Member names follow the same rules as in package.xml:
| Component | Member name format | Example |
|---|---|---|
| Apex class | Class name, no extension | `OrderHelper` |
| Apex trigger | Trigger name | `OrderTrigger` |
| Custom field | `Object.FieldName__c` | `Account.Region__c` |
| Custom object | `ObjectName__c` | `Invoice__c` |
| Flow | Flow API name | `Update_Order_Status` |
Do not use the wildcard * in a destructive changes file. The platform does not support it for deletion, so name every member explicitly. Naming members one by one also forces you to review exactly what you are about to remove.
The file still needs a package.xml
A destructive deployment must be accompanied by a manifest, even when you restore nothing. The manifest can have no types, as long as it declares an API version:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>When you are also restoring old versions, this file lists those components instead.
Pre-destructive or post-destructive
Salesforce CLI accepts two flags, and each names a destructive file that runs at a different point in the deployment:
| Flag | Conventional file name | When the deletion happens |
|---|---|---|
| `--pre-destructive-changes` | `destructiveChangesPre.xml` | Before the components in `package.xml` are deployed |
| `--post-destructive-changes` | `destructiveChangesPost.xml` | After the components in `package.xml` are deployed |
Both run inside one deployment. The platform treats the whole thing as a unit, so a failure anywhere cancels the deletions and the updates together.
Which one to use for a rollback
Rolling back usually needs the post-destructive file. Think about what depends on what. In the bad release, the old AccountService was changed to call OrderHelper. When you restore the old AccountService, the call disappears. If the deletion ran first, AccountService would still reference OrderHelper at that moment, and the delete would fail. Deleting after the restore makes the reference go away before the delete happens.
Use the pre-destructive file in the opposite situation. A component you are about to deploy conflicts with something that must be removed first. An example is a restored component whose name collides with a leftover one.
Restore and delete in one deployment
The usual rollback combines both jobs. Your package.xml lists the components the bad release changed, and the destructive file lists the components it added. Check out the reverted source, then run:
sf project deploy start --manifest manifest/package.xml --post-destructive-changes destructiveChangesPost.xml --target-org prod --test-level RunLocalTestsOne deployment gives you one atomic outcome. Either the org returns to the old state completely, or nothing changes and you still have the report to read. Doing the restore and the delete as two separate deployments leaves a window where the org is half rolled back.
Before running it against production, add --dry-run to the same command. The dry run checks the restores and the deletions, runs the tests, and commits nothing. That is a cheap way to catch a dependency problem before you are in the middle of an incident.
Deriving the delete list from Git
The list of added components is in your repository. Git can show which files the release merge added:
git diff --name-only --diff-filter=A <merge-commit-sha>^1 <merge-commit-sha> -- force-app<merge-commit-sha>^1 is the first parent, which is the main branch before the merge. --diff-filter=A keeps only files that were added. The output lists paths such as force-app/main/default/classes/OrderHelper.cls and the matching OrderHelper.cls-meta.xml. Convert each path to a member name using the table above. Both files for one class map to a single member.
Generate the list rather than remembering it. During an incident, people forget the small additions: a custom label, a permission set, a field added to a layout.
Dependencies, ordering and common failures
A delete fails if anything still references the component. The failure report names the blocker, and you read it the same way as any other deployment report. These are the cases you will meet most often:
- Apex referenced by other Apex. Delete the caller and the callee in the same file, or make sure the restored caller no longer references the class.
- Custom fields used elsewhere. Flows, validation rules, formulas, reports, page layouts, and Apex can all block a field delete. Restore the components that stop using the field in the same deployment, and put the field in the post-destructive file.
- Active flows. An active flow version cannot be deleted. Deactivate it first, or restore the previous active version. The flow chapter covers this in more detail.
- Components in a managed or released package. You cannot delete these through a destructive file.
- Order inside the file. The order of
<types>blocks does not fix dependency problems, so do not rely on it. Fix the references instead.
Deleting a field that the bad release added is usually safe, because the field existed only briefly. If users have already filled it in, the values are lost when you delete it. Check with the business before you delete a field that has been live for hours or days. Export its data first if it matters.
Verifying the result
After the deployment succeeds, confirm three things:
- The deployment report shows every restore and every delete as succeeded.
- The deleted components are gone from Setup, or return an error when you retrieve them.
- A fresh retrieve of the org matches your reverted Git commit. Retrieve into an empty directory and run
git diff --no-indexagainst your source to see any drift.
If a deleted component still appears, look for a second copy in another namespace, or for a change set or package that redeployed it.
Keep destructive files with the release
Treat the destructive file as part of the rollback plan, written before the release. When you prepare a release, write the destructiveChangesPost.xml that would undo it and keep it next to the manifest in the repository. Before the release, test it in a sandbox: deploy the release, then deploy the rollback. If the rollback fails there, you have found the dependency problem on a calm afternoon instead of during an outage.
Exercise
Release 42 changed the Apex class AccountService and added a new Apex class OrderHelper. The new AccountService calls OrderHelper. You revert the merge in Git and want to roll back in one deployment. Which destructive file should list OrderHelper, pre or post, and why? What happens if you pick the other one?
Show answer
List OrderHelper in destructiveChangesPost.xml and pass it with --post-destructive-changes. The deployment first restores the old AccountService, which no longer calls OrderHelper, and then deletes OrderHelper. If you used the pre-destructive file, the delete would run while the bad AccountService still referenced OrderHelper, so the delete would fail on the dependency and the whole deployment would be cancelled.
Exercise
A teammate wants to roll back by putting a wildcard member in the destructive file so that every Apex class added since last month is deleted. Give two reasons this is a bad plan.
Show answer
The platform does not support wildcards in destructive changes, so the deletion would not behave as intended. Even without that limit, a broad delete list would remove classes that other, unrelated releases added, and Apex deletions cannot be undone from the org. The list should come from the specific release merge, for example with git diff --diff-filter=A, and every member should be named.