Sub topic 12
A safe workflow: validate, review, and track deletions
This chapter ties the earlier material together into one repeatable process for deleting metadata. It assumes you can already write a package.xml, write destructive changes files, and run sf project deploy start. It also assumes you have a Salesforce DX project under version control with Git.
Deleting metadata is the riskiest thing a manifest can do. A bad retrieve costs you a few seconds. A bad deletion can remove a field and its data, and it can break Apex or flows that you did not know depended on it. The workflow below reduces that risk by making each deletion small, reviewed, tested in a sandbox, and recorded in Git.
The workflow at a glance
The process has six steps. Each step catches a different kind of mistake.
- Find out what depends on the component.
- Write the deletion in a destructive changes file and commit it on a branch.
- Review the change in a pull request.
- Validate with
--dry-runagainst a sandbox. - Deploy to the sandbox and test.
- Repeat the same deployment against production, then record the result.
Step 1: Find dependencies before you delete
Most failed deletions come from a dependency you did not know about. Look for references before you write any XML.
Search your local source first. This finds Apex, Lightning components, flows, layouts, and permission sets that mention the component by name.
grep -rn "OldRegion__c" force-appFor a field named Account.OldRegion__c, expect hits in these places:
- Apex classes and triggers that read or write the field.
- Page layouts, Lightning pages, and record types.
- Permission sets and profiles that grant field-level security.
- Flows, validation rules, formula fields, and reports.
Local source only shows what is in your project. The org may contain components that were never retrieved. In Setup, open the object's field and use the Where is this used? button to list references in the org. Reports and list views often appear there but not in your repository.
Plan the order of deletions from what you find. If a class uses a field, delete the class first or edit it to remove the reference. That ordering decision is what the pre and post destructive files are for.
Step 2: Write the destructive file on a branch
Create a branch so the deletion is isolated and reviewable.
git switch -c remove-old-region-fieldEdit each referencing component so it no longer uses the field, then list the field in a destructive file. Choose the file by the order you need:
| Situation | File to use | Reason |
|---|---|---|
| Delete a component that deployed components still reference | `destructiveChangesPre.xml` | The old component goes first, so the new components do not conflict with it. |
| Delete a component that only the old version of other components used | `destructiveChangesPost.xml` | New components deploy first and stop using it, then it can be removed. |
For this chapter's example, the updated Apex class no longer reads OldRegion__c. The class is deployed first, so the field is removed after it. That makes it a post destructive deletion.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Account.OldRegion__c</members>
<name>CustomField</name>
</types>
</Package>Save this as manifest/destructiveChangesPost.xml. The deployment also needs a manifest of what to deploy alongside it. Here it lists the updated class:
<?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>Keep a few rules in mind while writing:
- List each component by its exact name. A destructive changes file does not support the
*wildcard. - Use the
Object.FieldNameform for custom fields. - Keep the deletion small. One logical change per branch makes failures easy to trace.
- Do not put a component in both the manifest and a destructive file. The result is ambiguous.
If the deletion is the only thing you are deploying, use an empty package.xml that contains just the version element, as shown earlier in the tutorial.
Step 3: Review in a pull request
Commit the destructive file and the edited source together.
git add manifest/destructiveChangesPost.xml manifest/package.xml force-app
git commit -m "Remove Account.OldRegion__c and its Apex usage"
git push -u origin remove-old-region-fieldA reviewer should check these points:
- Each member name is spelled exactly, including the
__csuffix and the object prefix. - The metadata type in
<name>matches the component (CustomField, notCustomObject). - No wildcard appears in the destructive file.
- No remaining source file references the deleted component. A reviewer can run the same
grepas in step 1. - The choice of pre or post matches the dependency order.
- The deletion is intended for this release. Destructive files that linger can delete things again later.
Reviewing in a pull request also gives you a record of who approved the deletion and why. That record matters when someone asks months later where a field went.
Step 4: Validate with a dry run
A dry run sends the full deployment to the org and runs the checks, but saves nothing. Run it against a sandbox that mirrors production as closely as you can get. A full or partial copy sandbox is best because it contains the same dependent components.
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--dry-run \
--target-org my-sandboxRead the result carefully. A passing dry run means the Metadata API accepted the names and found no blocking dependency at that moment. It does not mean the deletion is harmless. It cannot tell you that a user depends on the data in the field, or that an integration reads it.
If the dry run fails, the error usually names the blocking component. Typical causes:
- A name is wrong, so the component is not found.
- A flow, layout, formula, or Apex class still references the component.
- A component was deleted already in another deployment.
Fix the cause on your branch, commit, and run the dry run again. Do not skip to a real deployment to see what happens.
Dry-run validation also depends on the org's state. A sandbox that was refreshed months ago may lack a flow that exists in production. Check the sandbox's age if production behaves differently from your validation.
Step 5: Deploy to the sandbox and test
Remove --dry-run and run the real deployment in the sandbox.
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxThen test the behavior the deleted component touched:
- Run the Apex tests for classes that handled the object.
- Open the record pages and check that layouts still load.
- Run any reports or flows that used the object.
- Confirm the component is gone in Setup.
Deleted custom fields go to a deleted state first and can be restored from the Recycle Bin until they are purged. Treat that as a safety net for the sandbox, not a plan for production. The restore window is limited, and restoring does not bring back everything that referenced the field.
> If you need the data, export it before deleting.
Export field data with Data Loader or a SOQL query before you deploy to production. A field's values are lost once the field is purged, and the deployment does not warn you about that.
Step 6: Deploy to production and record the result
Production gets the same files that passed in the sandbox. Do not edit them between environments. Run a dry run first, then the real deployment.
sf project deploy start \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--dry-run \
--target-org my-productionIf that passes, run the same command without --dry-run. Production deployments that include Apex changes run local tests, so allow time for them. Choose a quiet period, and tell the people who own affected reports, integrations, or automations.
After the deployment succeeds, finish the record keeping:
- Merge the pull request so the main branch reflects the org.
- Remove the destructive file from the main branch, or move it to an archive folder. A destructive file that stays in the repository and in your pipeline will be applied again on the next run. Deleting a component that is already gone usually produces a warning, but a component recreated later could be deleted by mistake.
- Note the deployment ID and date in the pull request or release notes.
The Git history now answers three questions: what was deleted, who approved it, and when.
Using `sf project delete source` safely
The sf project delete source command deletes a component from the org and from your local project in one step. It suits a quick cleanup in a sandbox or scratch org.
sf project delete source \
--metadata ApexClass:LegacyAccountService \
--target-org my-sandboxIt is a poor fit for production work because it leaves no reviewable file behind. The deletion happens as you run the command, with no step for a second person to look at it. For production, prefer a destructive file in a pull request. If you do use the command, commit the local file removals it makes so your repository stays in sync with the org.
The two approaches compare like this:
| Aspect | Destructive changes file | `sf project delete source` |
|---|---|---|
| Reviewable before running | Yes, as a file in a pull request | No |
| Repeatable across orgs | Yes, same file for each org | No, you re-run the command |
| Updates local source | No, you delete files yourself | Yes |
| Best for | Production and shared sandboxes | Scratch orgs and personal sandboxes |
Because a destructive file does not touch local source, remember to delete the matching files from force-app in the same pull request. Otherwise the next deployment of your whole project will recreate the component you just removed.
Common mistakes in the workflow
- Deleting source files but not the org component. Removing a file from
force-appdoes not delete anything in the org. The org keeps the component until a destructive deployment removes it. - Deleting from the org but leaving the source. The next full deploy puts the component back.
- Reusing an old destructive file. The file runs every time it is passed. Check what it contains before each use.
- Validating in an unlike sandbox. A developer sandbox with no data or automation can pass checks that production would fail.
- Skipping the data export. The deployment does not ask whether you still need the values.
Practice
Exercise
You removed Account.OldRegion__c from an Apex class and want to delete the field. The dry run in the sandbox passed. List the remaining steps before the production deployment is complete, and name one cleanup action afterward.
Show answer
Deploy to the sandbox for real and test the affected Apex, layouts, reports, and flows. Export the field data if it is needed. Run the same files with --dry-run against production, then run the real production deployment. Afterward, merge the pull request, delete the matching source files from force-app if not already done, and remove or archive the destructive file so it is not applied again.
Exercise
A teammate uses sf project delete source to remove a class from production and does not commit anything. What are two problems with this?
Show answer
The deletion had no review step, so nobody checked dependencies or approved it. Also, the local file removal is uncommitted, so the repository does not match the org, and another developer's next deploy from the main branch will recreate the class.