Sub topic 8
Deleting added components with destructive changes
This chapter assumes you can read a package.xml, deploy with the Salesforce CLI, and use git revert. You also need the earlier chapter on redeploying the previous version.
Why redeploying is not enough
Redeploying the previous version restores components that existed before the release. It does not remove components the release created. If the bad release added an Apex class, a custom field, or a flow, the old source has no copy of it. Deploying the old source leaves the new component in the org.
A destructive changes manifest closes that gap. It is an XML file that lists components to delete from the org during a deployment.
Writing the manifest
A destructive changes file has the same structure as package.xml. Each types block names a metadata type and the members to delete. This example removes an Apex class added by the bad release:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>OrderHelper</members>
<name>ApexClass</name>
</types>
<version>60.0</version>
</Package>Some rules to follow:
- Wildcards (
*) are not supported. List every component by name. - Fields use the object prefix, for example
Account.Region__cunderCustomField. - Listing a component that is already gone does not cause an error in most cases. Check the deploy report to confirm.
- Find what to delete by comparing the release's
package.xmlandgit diff --name-statuswith the previous release. Anything markedA(added) is a candidate.
Pre or post: choosing the order
Salesforce supports two files, and the choice matters.
| File | When it runs | Typical use |
|---|---|---|
| `destructiveChangesPre.xml` | Before the rest of the deployment | Remove a component that blocks the new deployment |
| `destructiveChangesPost.xml` | After the rest of the deployment | Remove components after the restored versions are in place |
For a rollback, use the post file. Salesforce refuses to delete a component that other components still reference. Suppose the bad release added the class OrderHelper, and a trigger changed in the same release calls it. If you delete the class first, the delete fails because the bad trigger still references it. If you restore the old trigger first, the reference disappears, and the class can then be deleted.
Running the rollback
Revert the release merge commit, then deploy the reverted source together with the post-destructive file. The --manifest flag is required when you use --post-destructive-changes.
git revert -m 1 <merge-commit-sha>
sf project deploy start --manifest manifest/package.xml --post-destructive-changes destructiveChangesPost.xml --target-org prod --test-level RunLocalTestsBecause this is one deployment, it follows the all-or-nothing rule from the first chapter. If a test fails or a delete is blocked, nothing is applied and the org stays as it was. You can also run sf project deploy validate with the same flags first, to check the rollback before you commit to it.
Common errors
- A dependency blocks the delete. The error names the component that still references yours. Add that component to the restored source, or delete it in the same file.
- The manifest is rejected. Check the XML namespace and that each
typesblock has anameelement. - The delete is not applied. Make sure you passed
--post-destructive-changeswith a path that exists. A misspelled file name stops the command before it runs. - The restored source references the removed component. Review the reverted code before you deploy.
What deletion does not undo
Deleting a custom field also deletes the data stored in it. Salesforce keeps deleted custom fields in a recycle state for a limited time, but you should not rely on that as a backup. If the field already holds data you need, export it first. The later chapter on irreversible changes covers this in more detail.
Exercise
A release added the Apex class OrderHelper and changed the trigger OrderTrigger to call it. You revert the release in Git and need to remove OrderHelper. Should you list it in destructiveChangesPre.xml or destructiveChangesPost.xml, and why?
Show answer
Use destructiveChangesPost.xml. The reverted OrderTrigger must be deployed first so that it no longer references OrderHelper. A pre-destructive delete would run while the bad trigger still references the class, so Salesforce would block it.