Sub topic 9
Deleting Source Components with the CLI Delete Command
This chapter covers sf project delete source, the command that removes a component from your org and from your local project in one step. It assumes you can read a package.xml, you have a Salesforce DX project with a force-app directory, and the sf CLI is authorized to a sandbox or scratch org. It also assumes you have seen how a destructive changes file works. Here you will see when the delete command is the better tool and when it is not.
What the command does
A destructive deployment, covered in earlier chapters, needs two files: a package.xml and a destructive changes file. You write them by hand, deploy them, and then remove the local source files separately. If you forget the last step, the next deploy puts the component back.
sf project delete source combines these steps. You name the components, and the CLI does three things:
- It finds the named components in your local project.
- It deletes them from the target org.
- It deletes the matching source files from your project.
The CLI builds the destructive deployment for you, so you never write a destructiveChanges.xml file for this case.
sf project delete source \
--metadata ApexClass:LegacyAccountService \
--target-org my-sandboxBefore it deletes anything, the command lists the components it found and asks you to confirm. The listing shows which files it will remove locally and which components it will remove from the org. Read that list every time. It is the cheapest safety check you have.
Two ways to name what you delete
You can select components by metadata name or by file path. These two options cannot be combined in one run.
By metadata name with --metadata
The --metadata flag (short form -m) takes a Type:Name pair. The type is the metadata type name from package.xml. The name follows the same member rules you learned for manifests.
sf project delete source \
--metadata ApexClass:LegacyAccountService \
--metadata CustomField:Account.OldRegion__c \
--target-org my-sandboxRepeat the flag to delete several components in one run. Three rules carry over from manifests:
- A custom field is written as
Object.FieldName, as inAccount.OldRegion__c. - The type name must match the metadata type exactly.
ApexClassworks.Apex Classandapexclassdo not. - A wildcard such as
ApexClass:*is not a safe way to delete things. Name each component.
The component must exist in your local project source. The command resolves names against your local files, so a component that exists only in the org is not something it can find. For that case, use a destructive changes file.
By file path with --source-dir
The --source-dir flag (short form -d) takes a path to a file or a directory.
sf project delete source \
--source-dir force-app/main/default/classes/LegacyAccountService.cls \
--target-org my-sandboxThe CLI works out which component the file belongs to. For an Apex class, it also treats the companion LegacyAccountService.cls-meta.xml file as part of the same component, so both files are removed.
If you pass a directory, every component inside it is a deletion candidate. That is powerful and easy to misuse.
sf project delete source \
--source-dir force-app/main/default/objects/Legacy_Order__c \
--target-org my-sandboxThis deletes the whole custom object Legacy_Order__c, including all of its fields, record types, and other child components in that folder. Check the confirmation list carefully before you accept a directory deletion.
Flags you will use
| Flag | Short | Purpose |
|---|---|---|
| `--metadata` | `-m` | Select components as `Type:Name`. Repeat the flag for more than one. |
| `--source-dir` | `-d` | Select components by file or directory path. |
| `--target-org` | `-o` | The org to delete from. |
| `--check-only` | `-c` | Validate the deletion without changing the org or your project. |
| `--no-prompt` | `-r` | Skip the confirmation prompt, for scripts. |
| `--test-level` | `-l` | Choose which tests run with the deletion. Used mainly for production. |
| `--wait` | `-w` | Minutes to wait for the deletion to finish. |
Flags change between CLI releases. Run sf project delete source --help to see the exact list for your installed version.
Validate first with --check-only
You already saw --dry-run on sf project deploy start. The delete command has its own validation flag, --check-only. It runs the same server-side checks as a real deletion, including dependency checks, but it does not delete anything from the org or from your project.
sf project delete source \
--metadata ApexClass:LegacyAccountService \
--target-org my-sandbox \
--check-onlyIf the validation fails, you get the same kind of error messages as in a failed destructive deployment, for example that another component still references the one you are deleting. Fix those problems before you run the real command.
The confirmation prompt and automation
By default the command pauses and asks you to confirm. In a CI pipeline there is nobody to answer, so you add --no-prompt.
sf project delete source \
--metadata ApexClass:LegacyAccountService \
--target-org my-sandbox \
--no-promptUse --no-prompt only after a validation step has already passed. Skipping the prompt removes your last chance to notice that a name resolved to more than you expected.
Following the full flow
The diagram shows what happens when you run the command, including the points where it stops.
The order matters. The CLI deletes from the org first. It removes local files only after the org deletion succeeds. If a dependency blocks the org deletion, your local files stay where they are, and you can try again after fixing the dependency.
Delete command or destructive changes file?
Both approaches delete metadata through the Metadata API. They differ in how much control you get and what they leave behind.
| Question | `sf project delete source` | Destructive changes file |
|---|---|---|
| Needs the component in local source? | Yes | No |
| Deletes local files? | Yes | No, you remove them yourself |
| Pre or post ordering? | Not selectable | You choose `Pre` or `Post` |
| Combine with a deploy in one run? | No | Yes, with `--manifest` |
| Leaves a reviewable file in version control? | No, only the file removals show up | Yes, the manifest file itself |
| Good for a release pipeline? | Poor fit | Good fit |
The delete command is a good fit for interactive cleanup. You are working in a scratch org or sandbox, you spot a class nobody uses, and you want it gone from the org and the project. It is a poor fit for release pipelines, for two reasons.
- A pipeline needs deletions to travel with the deployment. If a new field replaces an old one, the deploy and the deletion should happen in a defined order, which only a pre or post destructive file gives you.
- A pipeline needs the same deletion to run against several orgs: integration, UAT, production. A destructive changes file is a file you commit and reuse. The delete command removes local source immediately, so the second org has nothing left to resolve against.
Worked example
Suppose your project has an unused class and a field you replaced last sprint. First, confirm what is in your source.
ls force-app/main/default/classes | grep Legacy
ls force-app/main/default/objects/Account/fields | grep OldRegionThen validate against the sandbox.
sf project delete source \
--metadata ApexClass:LegacyAccountService \
--metadata CustomField:Account.OldRegion__c \
--target-org my-sandbox \
--check-onlyIf validation passes, run the same command without --check-only. Read the confirmation list. It should show exactly two components, with the paths of the three files that will go: the .cls file, its -meta.xml file, and the field's .field-meta.xml file. Accept the prompt.
Afterward, check the project with Git.
git statusYou should see three deleted files. Commit them with a message that names the removed components. If you also need the deletion to happen in other orgs later, generate a destructive changes file for those orgs at this point. Your local source no longer has the files, so the delete command cannot find them again.
Common problems
The component is not found
The error says the component cannot be found in the project. Common causes:
- The name is misspelled or the type name has the wrong case.
- The field is written as
OldRegion__cinstead ofAccount.OldRegion__c. - The file is outside your package directories. Check
packageDirectoriesinsfdx-project.json. - You already deleted the local file by hand, so the CLI has nothing to resolve. Use a destructive changes file to remove it from the org.
The org deletion is blocked by a dependency
Salesforce refuses to delete a component that another component still uses. A custom field referenced by an Apex class, a flow, a validation rule, or a page layout is a typical case. The CLI reports the blocking component. Remove or update the reference first, deploy that change, and then delete. The dependency chapter covers this in more detail.
Local source and org drift apart
Because the command deletes from the org and the project in one step, running it against the wrong org is easy to do. If --target-org is omitted, the CLI uses your default org. Always pass --target-org explicitly, and confirm the alias with sf org list when you work with more than one org.
Summary
sf project delete sourcedeletes components from the org and from your local project in one command.- Select components with
--metadata Type:Nameor--source-dir path, but not both in the same run. - The component must exist in your local source for the CLI to find it.
- Use
--check-onlyto validate, and--no-promptonly in automation after validation has passed. - For release pipelines, multi-org rollouts, or ordering against a deploy, use pre and post destructive changes files instead.
Exercise
A teammate removed the file force-app/main/default/classes/OldHelper.cls by hand and committed it. They now run sf project delete source --metadata ApexClass:OldHelper --target-org my-sandbox to remove the class from the sandbox, and the command reports that it cannot find the component. Why does it fail, and what should they do instead?
Show answer
The delete command resolves names against local source, and the local files are already gone, so there is nothing to match. They should write a destructive changes file that lists OldHelper under the ApexClass type, pair it with a package.xml, and run sf project deploy start with --post-destructive-changes (or --pre-destructive-changes), validating first with --dry-run.
Exercise
You need to remove an unused custom field from a sandbox and from your project, and you want to be sure no other component still references it before anything changes. Write the command you would run first, and say what the next step is if it succeeds.
Show answer
sf project delete source --metadata CustomField:Account.OldRegion__c --target-org my-sandbox --check-only. This validates the deletion, including dependency checks, without changing the org or the project. If it succeeds, run the same command without --check-only, read the confirmation list, accept the prompt, and commit the deleted source files.