Sub topic 9
Deleting Source Components with the CLI Delete Command
This chapter assumes you can write a package.xml, know the Type:Name form of a metadata name, and have run sf project deploy start --manifest. You also need a DX project with a force-app directory and an authorized sandbox.
Why a second way to delete
The previous chapters removed metadata with destructive changes files. That approach works well for reviewed, repeatable releases. It has a cost: you hand-write XML, and the files in your project stay put. You must delete them from source yourself, or the next deploy puts the components back.
sf project delete source does both jobs in one command. It deletes the named components from the org and removes their files from your local project.
Running the command
Name a component with --metadata using the Type:Name format:
sf project delete source \
--metadata ApexClass:LegacyAccountService \
--target-org my-sandboxThe command lists what it will delete and asks you to confirm. After you confirm, it sends a deletion to the org and then removes the local files, such as the .cls file and its -meta.xml file.
You can pass --metadata more than once, or point at files with --source-dir:
sf project delete source \
--source-dir force-app/main/default/classes/LegacyAccountService.cls \
--source-dir force-app/main/default/objects/Account/fields/OldRegion__c.field-meta.xml \
--target-org my-sandboxCustom fields use the same Object.FieldName naming you used in the manifest. Written as a flag value, that is CustomField:Account.OldRegion__c.
Useful flags
| Flag | Effect |
|---|---|
| `--metadata`, `-m` | Names a component as `Type:Name`. Repeat the flag for more components. |
| `--source-dir`, `-d` | Selects components by file or directory path. |
| `--check-only`, `-c` | Validates the deletion without saving changes to the org or the project. |
| `--no-prompt`, `-r` | Skips the confirmation. Use it only in scripts you trust. |
| `--test-level` | Sets which Apex tests run. This matters in production. |
Run sf project delete source --help to see the full list for your CLI version.
What happens behind the scenes
The command builds a destructive deployment for you. It is the same Metadata API operation you wrote by hand earlier. Because of this, the same rules apply. A component that other components depend on still cannot be deleted. A wrong name still fails.
The command runs as follows:
Do not include that block. Use the diagram below instead.
If the org rejects the deletion, your local files stay in place. You can then read the error, fix the cause, and run the command again.
When to use it and when not to
Use the delete command when you are cleaning up a sandbox or a scratch org, and you want your project to match the org right away. It is quick, and it avoids the mistake of leaving files behind.
Prefer destructive changes files when any of these apply:
- You are releasing to production through a pipeline. A reviewed XML file in version control is an audit trail. A command typed in a terminal is not.
- You need pre-destructive deletions, which run before the deploy. The delete command has no pre or post choice.
- Several environments must receive the same deletion in the same order.
Source tracking changes the picture. In an org with source tracking, such as a scratch org, the usual route is to delete the files locally and deploy. The delete command is aimed at orgs without tracking, such as most sandboxes.
Common errors
- Wrong type or name.
ApexClass:LegacyAccountService.clsfails. Drop the file extension. - Dependency failure. Another class, flow, or layout still refers to the component. Remove or update that reference first, then run the command again.
- Deleting from the wrong org. The command acts on the org named by
--target-org, or on your default org if you omit it. Check withsf org listbefore you confirm. - Local changes lost. The files are removed from your working directory. Commit your work first so you can restore them with
git.
Run it with --check-only first. It reports the same dependency errors as a real run, and it changes nothing.
sf project delete source \
--metadata ApexClass:LegacyAccountService \
--check-only \
--target-org my-sandboxExercise
You ran sf project delete source --metadata ApexClass:OldHelper.cls --target-org my-sandbox and it failed with a name error. What is wrong, and how do you test the corrected command without changing the org or your project?
Show answer
The member name must not include the file extension, so use ApexClass:OldHelper. To test the corrected command safely, add --check-only. It validates the deletion and reports any dependency errors, without changing the org or removing local files.