Sub topic 6
Writing a destructive changes file
This chapter assumes you can read a package.xml manifest (types, members, name, version) and know the metadata type names, such as ApexClass and CustomField. It also assumes you have a Salesforce DX project and an authorized sandbox. Here you will write the file that lists what to delete. Later chapters cover running it and choosing between pre and post.
What the file is
A destructive changes file is an XML file in the same format as package.xml. A normal manifest tells the Metadata API what to retrieve or deploy. A destructive changes file lists components to delete from the target org.
Each types block holds one metadata type in name and one or more members to delete. The Metadata API deletes only what you name.
<?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>This file deletes the Apex class LegacyAccountService and the custom field OldRegion__c on Account. The version element is optional here. The manifest that accompanies the file sets the API version for the deployment.
File names
The CLI lets you pass the file by any path, but the conventional names show what each file does:
| File name | Deletes |
|---|---|
| `destructiveChangesPre.xml` | Before the deployment saves new components |
| `destructiveChangesPost.xml` | After the deployment saves new components |
| `destructiveChanges.xml` | Same timing as the post file (the older name) |
Pick the timing in the next chapter. For now, the content of each file is written the same way.
Name members exactly
Deletion is by exact name, so a typo means the component is not deleted. Follow the same rules as in a manifest:
- A field is written as
Object.FieldName, for exampleAccount.OldRegion__c. - Custom objects and fields keep their
__csuffix. - Names are the API names, not labels. Use
LegacyAccountService, not "Legacy Account Service". - Components in folders, such as reports, are written as
FolderName/ComponentName.
If you are unsure of a name, check the source folder in your project. The file name without its suffix is usually the member name.
Pair it with a package.xml
A deployment needs a manifest, even when it only deletes. If you have nothing to deploy, use a package.xml with no types:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>If you do deploy components at the same time, put them in the normal package.xml. Never list a component in both the manifest and the destructive file. That asks the API to save and delete the same thing.
A first run
Put both files in your manifest folder, then run a deployment against a sandbox:
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxThe --post-destructive-changes flag points at your file. The --pre-destructive-changes flag works the same way. You will compare them in the next chapter.
Common mistakes
- Using a label or a partial name. The member must match the API name.
- Writing a field as
OldRegion__cinstead ofAccount.OldRegion__c. - Using
*. It is not valid for deletion. - Forgetting the paired
package.xml, so the deployment has no manifest. - Deleting only from the org. Your local source still has the file, so the next deploy can bring the component back. Remove it from source and version control too.
Exercise
You want to delete the custom field Region__c on the Contact object and the Apex class OldHelper. Write the members and types for the destructive changes file, and say which entry would be invalid if you replaced OldHelper with *.
Show answer
Two types blocks. The first has <members>OldHelper</members> and <name>ApexClass</name>. The second has <members>Contact.Region__c</members> and <name>CustomField</name>. Replacing OldHelper with * would be invalid because wildcards are not supported in destructive changes files, so every component must be named exactly.