Sub topic 11
Handling deletes and renames
This chapter assumes you have read the earlier chapters on previewing, deploying, and retrieving tracked changes, and that you have a scratch org with tracking and a Git-managed DX project. Deletes and renames are the changes people most often get wrong, because the CLI treats them differently from edits.
Why deletes and renames need attention
An edit changes a file that exists on both sides. A delete removes the file on one side, and a rename removes one name and adds another. If you don't know how the CLI interprets these changes, you can leave stale components in the org, or destroy components you meant to keep.
Two facts drive everything below:
- The CLI identifies a component by its metadata type and full name, for example
ApexClass:OrderHelper. It does not identify a component by what the file contains. - Whether a deletion travels to the other side depends on whether the org has source tracking.
Deletes in a tracked org
In a tracked org, such as a scratch org or a tracking-enabled sandbox, the CLI sees a missing local file as a pending delete. Remove the file, then preview:
rm force-app/main/default/classes/OrderHelper.cls
rm force-app/main/default/classes/OrderHelper.cls-meta.xml
sf project deploy preview --target-org my-scratchThe preview lists the component in its delete section. The exact heading depends on your CLI version, but it is a separate group from the components that will be deployed. Delete both files for an Apex class. If you leave the -meta.xml file behind, the leftover file causes confusing results.
Deploying with no source flags then deletes the component in the org:
sf project deploy start --target-org my-scratchDeletions travel in the other direction too. If someone deletes a component in the org, sf project retrieve start removes the matching local files. Preview first with sf project retrieve preview, because retrieve can delete files in your working directory.
The explicit delete command
You can also delete a component from the org and your project in one step:
sf project delete source --metadata ApexClass:OrderHelper --target-org my-scratchThe command prompts for confirmation. Add --no-prompt in scripts. Add --check-only to validate the delete without applying it. This command works against any org, tracked or not, which makes it useful for the untracked case described later.
Renames are a delete plus an add
The CLI has no rename operation. When you rename a component, the old name looks deleted and the new name looks added. The preview shows one delete and one deploy, and a deploy performs both.
Here is a rename of an Apex class from OrderHelper to OrderService:
cd force-app/main/default/classes
git mv OrderHelper.cls OrderService.cls
git mv OrderHelper.cls-meta.xml OrderService.cls-meta.xmlRenaming the files is not enough. The class name inside the file must change as well:
public with sharing class OrderService {
public static Integer countOpenOrders() {
return [SELECT COUNT() FROM Order WHERE Status = 'Draft'];
}
}Then preview and deploy:
sf project deploy preview --target-org my-scratch
sf project deploy start --target-org my-scratchThe preview should show OrderService as a deploy and OrderHelper as a delete. If the old name does not appear as a delete, check that you renamed the files and did not copy them.
Renames hide dependencies
Because a rename is a delete, the org enforces its delete rules. A delete fails if another component still references the old name, and the whole deploy can fail with it. Typical blockers:
- Another Apex class, trigger, or Flow that calls
OrderHelper. - A Lightning page or layout that references a renamed component.
- A custom field used in a formula, validation rule, or report.
Update every reference in the same deploy. If the deploy reports a dependency error, search your project for the old name:
grep -rn "OrderHelper" force-appRenaming data-bearing components
Deleting a custom field or custom object removes its data, and a rename of the API name is a delete plus an add. The new field starts empty. Salesforce keeps deleted custom fields in a recoverable state for a limited time, but do not rely on that as a plan. For fields and objects that hold data, rename in the Setup UI where Salesforce migrates the data, then retrieve the result. Do not rename the local file and deploy.
Deletes and renames in an untracked org
An org without tracking, such as a Developer Edition org, a production org, or a sandbox without tracking, has no snapshot to compare against. The CLI cannot see that a local file was removed. A deploy by source directory, metadata name, or manifest sends only what exists locally, so a component you deleted locally stays in the org.
This is the most common cause of "I deleted it but it is still there."
You have two options.
Option 1: delete source
sf project delete source --metadata ApexClass:OrderHelper --target-org my-sandboxThis removes the component from the org and from your local project.
Option 2: destructive changes manifest
For several deletes, or for a change you want in version control, list the components in a destructive changes file:
<?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>62.0</version>
</Package>Save it as manifest/destructiveChangesPost.xml. Deploy it with a manifest that holds the components you are adding. For a rename, the manifest lists the new class and the destructive file lists the old one:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>OrderService</members>
<name>ApexClass</name>
</types>
<version>62.0</version>
</Package>sf project deploy start --target-org my-sandbox \
--manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xmlThe flag you choose sets the order:
| Flag | When the deletes run | Use it when |
|---|---|---|
| `--pre-destructive-changes` | Before the deploy | The old component blocks the new one, for example a name collision |
| `--post-destructive-changes` | After the deploy | Other components must first stop referencing the old one, which is the usual case for renames |
For a rename, use the post flag. The new component and the updated references deploy first, and then the old component is removed with nothing left pointing at it.
Retrieving org-side deletes without tracking
Retrieve in an untracked org adds and updates files but does not remove local files for components deleted in the org. Delete those files yourself, then commit the change.
Behavior at a glance
| Situation | Tracked org | Untracked org |
|---|---|---|
| Local file deleted, then `deploy start` | Component deleted in the org | Nothing happens in the org |
| Component deleted in org, then `retrieve start` | Local files removed | Local files remain |
| Local rename, then `deploy start` | New name added, old name deleted | New name added, old name stays |
| Explicit delete | `delete source` or deploy | `delete source` or destructive changes file |
Conflicts involving deletes
A delete can conflict like any other change. If you delete OrderHelper locally while someone edited it in the org, the preview marks it as a conflict. You have to decide whether the org edit matters before you proceed.
- Run
sf project deploy previewand note the conflict. - Run
sf project retrieve previewto see what changed in the org. - Retrieve the component if you want to inspect the org edit, or decide the delete is correct.
- Use
--ignore-conflictsonly after you have decided the org version can be discarded.
Common errors
- The component is still in the org after a deploy. The org is untracked, or you used
--source-diror--manifest. Usedelete sourceor a destructive changes file. - The deploy fails with a dependency error. Another component still references the deleted or old name. Find the references and update them in the same deploy.
- Retrieve deleted files you wanted. Someone removed the component in the org. Restore the files from Git with
git restoreand reconcile the org. - The preview shows an unexpected delete. A file was moved outside the package directories or ignored by
.forceignore. Check both before deploying.
Exercise
You renamed the Apex class LeadScorer to LeadRanker locally, in a sandbox without source tracking. You ran sf project deploy start --source-dir force-app/main/default/classes. The deploy succeeded, but the org now has both classes. Why, and how do you fix it?
Show answer
The sandbox has no tracking, so the CLI cannot detect that LeadScorer was removed locally. The deploy only added LeadRanker. To remove the old class, run sf project delete source --metadata ApexClass:LeadScorer --target-org <alias>. Alternatively, list LeadScorer in a destructive changes file and deploy with --post-destructive-changes. After that, check that no other component references LeadScorer.
Exercise
Why should you preview before running sf project retrieve start in a tracked org when you suspect a teammate deleted components in the scratch org?
Show answer
In a tracked org, retrieve also applies deletions from the org to your local project. The preview lists those deletes first, so you can see which local files would be removed before any change is made.