Sub topic 11
Handling deletes and renames
This chapter assumes you can preview, deploy, and retrieve tracked changes, as covered earlier. Deletes and renames behave differently from edits, because the CLI has to remove something from the other side.
How tracking sees a delete
In a tracked org, deleting a component's files locally counts as a change, just like editing one. The next deploy removes the component from the org. The reverse also holds. If someone deletes a component in the org, the next retrieve removes the matching local files.
Always preview first. The output lists deletions in their own section.
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchLook for the "Will Delete" entries. A deletion you did not expect often means a file was moved or removed by accident.
sf project deploy start --target-org my-scratchYou can also delete a component from both places in one step:
sf project delete source --target-org my-scratch --metadata ApexClass:OldHelperThe command asks for confirmation before it removes anything.
Renames are a delete plus an add
The CLI does not track a rename as a single event. Renaming a component produces two changes: the old name is deleted and the new name is added. A preview of a renamed Apex class shows both.
Rename the file and its companion file together, such as NewName.cls and NewName.cls-meta.xml. Also update the name inside the source, such as the class declaration. A mismatch makes the deploy fail.
When the org refuses a delete
A delete can fail when another component depends on the target. Examples are a trigger that calls a class you removed, or a page layout that references a deleted field. The deploy reports the dependency and rolls back. Delete or update the dependent component in the same deploy, then try again.
Deletes in orgs without tracking
An org without tracking cannot detect removed files, so deleting locally does nothing remotely. Use a destructive changes manifest. It has the same format as package.xml and lists what to remove.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>OldHelper</members>
<name>ApexClass</name>
</types>
</Package>Save it as destructiveChanges.xml and pass it with a manifest:
sf project deploy start --target-org my-sandbox --manifest manifest/package.xml --post-destructive-changes manifest/destructiveChanges.xmlUse --pre-destructive-changes instead when the deletion must happen before the deploy, for example to remove a component that blocks a new one.
Quick reference
| Situation | What the CLI does |
|---|---|
| Local file deleted, tracked org | Deploy deletes it in the org |
| Component deleted in tracked org | Retrieve deletes the local files |
| File renamed locally | Delete of old name, add of new name |
| Delete in untracked org | Needs a destructive changes manifest |
Exercise
You renamed the Apex class OrderHelper to OrderService in a scratch org project, including both files. What will sf project deploy preview show, and why?
Show answer
It shows OrderHelper under "Will Delete" and OrderService under "Will Deploy". Tracking has no rename event, so a rename is recorded as a delete of the old component plus an add of the new one.
Exercise
You delete a class file in a sandbox without tracking and run a normal deploy. The class is still in the org. How do you remove it?
Show answer
Without tracking, the CLI cannot see the removed file. List the class in a destructiveChanges.xml file and deploy with --post-destructive-changes (or --pre-destructive-changes if the deletion must come first).