Sub topic 7
Pre versus post destructive changes
This chapter assumes you can read a package.xml, you have the Salesforce CLI (sf) authorized to a sandbox, and you know how to write a destructive changes file. The question here is when each deletion runs relative to the rest of the deployment.
Two files, two moments
A Metadata API deployment can carry two destructive files besides package.xml:
destructiveChangesPre.xmldeletes components before the deployment saves the contents ofpackage.xml.destructiveChangesPost.xmldeletes components after the contents ofpackage.xmlhave been deployed.
Both files use the same format as package.xml, list exact member names, and do not support the * wildcard. The only difference is timing.
The timing matters because Salesforce refuses to delete a component that another component still uses. Your choice of file decides whether the reference is still there when the deletion runs.
When to use the post file
Use the post file when your deployment also changes the components that depend on the one you are deleting. This is the most common case.
Suppose Account.OldRegion__c is referenced by the Apex class AccountService. You edit the class so that it no longer uses the field. You then deploy the class and delete the field in the same run.
- With a pre file, the field deletion runs while the old class is still in the org. It fails because of the dependency.
- With a post file, the new class is deployed first. The field has no remaining references when the deletion runs.
Deleting a component that your deployment replaces or stops using belongs in destructiveChangesPost.xml. Treat the post file as the default and switch to pre only for a specific reason.
When to use the pre file
Use the pre file when something in the org blocks the deployment, and removing it first clears the way. Typical cases:
- A component with the same name exists with a different shape. For example, you want to recreate a component, and the old version conflicts with the new one.
- An existing component would fail validation against the new components in
package.xml. For example, an old class has the same name as a class you are adding in a different form. - You need to remove something so that a later step in the same deployment can succeed.
Pre deletions run against the org as it is now. Anything that still references the deleted component, including components you are about to update, can make the deletion fail. Use the pre file only when nothing in the org depends on the deleted component, or when you delete the dependents in the same pre file.
Running both files
The CLI takes each file through its own flag. Both need a manifest, so you also pass --manifest.
sf project deploy start \
--manifest manifest/package.xml \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxIf you only delete components, the manifest can be an empty package.xml that contains just a version:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>Validate before you commit to an order. Adding --dry-run checks the deployment, including the pre deletions, without saving changes to the org.
sf project deploy start \
--manifest manifest/package.xml \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--dry-run \
--target-org my-sandboxIf a dry run fails with a dependency error, read the message. It names the component that still references your target. Then decide whether the dependent is part of this deployment (move the deletion to the post file) or already in the org (delete it first, or update it in an earlier deployment).
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Field deletion fails because a class uses it | Field is in the pre file, and the class is updated in the same deployment | Move the field to the post file |
| New component fails because an old one conflicts | Old component is still in the org when `package.xml` deploys | Move the old component to the pre file |
| Deletion has no effect | Member name is wrong or the component does not exist | Check the exact name, such as `Account.OldRegion__c` |
| Error about a wildcard | `*` used in a destructive file | List each component by name |
Exercise
Your deployment updates the Apex class AccountService so it no longer reads Account.OldRegion__c, and it also deletes that field. In which destructive file should the field go, and why?
Show answer
Put Account.OldRegion__c in destructiveChangesPost.xml. The post file runs after the updated AccountService is deployed, so the field no longer has a reference when it is deleted. In the pre file, the old class would still reference the field and the deletion would fail.
Exercise
Which command-line flag passes the file that runs before the deployment, and what does --dry-run add to it?
Show answer
The flag is --pre-destructive-changes. Adding --dry-run validates the deployment, including those pre deletions, without saving any changes to the org.