Sub topic 5
Retrieving and deploying with --manifest
This chapter assumes you can read package.xml, have a Salesforce DX project with a force-app directory, and have authorized an org with the sf CLI. You will use the manifest to move metadata between the org and your local project. The next chapters build on this to delete metadata.
What --manifest does
A manifest tells the CLI which components to include. Without one, you would name components one at a time with --metadata or point at folders with --source-dir. The --manifest flag (short form -x) takes the path to a package.xml file instead.
Both retrieve and deploy read the manifest the same way, but they act in opposite directions:
sf project retrieve startcopies the listed components from the org into your local project.sf project deploy startcopies the listed components from your local project into the org.
Retrieving with a manifest
Start with a manifest that lists what you want:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>AccountService</members>
<members>AccountServiceTest</members>
<name>ApexClass</name>
</types>
<types>
<members>Account.Region__c</members>
<name>CustomField</name>
</types>
<version>62.0</version>
</Package>Then run the retrieve:
sf project retrieve start \
--manifest manifest/package.xml \
--target-org my-sandboxThe CLI writes the files into your default package directory, usually force-app. If a file already exists locally, the org version overwrites it. In a project under version control, run git status first so you can review what changed.
The <version> element sets the API version used for the request. Some metadata types and fields only exist in newer versions, so a very old version can leave out components you expect.
Deploying with a manifest
Deploying uses the same flag:
sf project deploy start \
--manifest manifest/package.xml \
--target-org my-sandboxThe CLI looks for each listed component in your project's package directories, as defined in sfdx-project.json. It then sends those components to the org. Three behaviors are worth knowing:
- A component that is in the manifest but missing locally cannot be deployed. The CLI reports an error or deploys nothing for that entry.
- A component that is local but not in the manifest is ignored.
- A deploy only creates or updates components. It never removes a component from the org, even if you deleted the local file.
The third point is why destructive changes exist. Deleting AccountService.cls from your project and deploying leaves the class in the org.
You can't combine --manifest with --source-dir or --metadata in the same deploy command. Pick one way to select components.
Checking before you commit changes
Two flags make a manifest deploy safer:
sf project deploy start \
--manifest manifest/package.xml \
--dry-run \
--test-level RunLocalTests \
--target-org my-sandbox--dry-run validates the deployment and runs tests without saving anything. --test-level RunLocalTests runs the tests in your org, excluding those from managed packages. Production deployments that include Apex require tests, so validating in a sandbox first shows you failures early.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Retrieve returns nothing for a member | Member name misspelled, or wrong type in `<name>` | Check the exact name, such as `Account.Region__c` for a field |
| Deploy says no source-backed components found | Manifest lists components that are not in a package directory | Retrieve them first, or check `sfdx-project.json` |
| A field is missing after retrieve | The field is in the org but not in the manifest | Add it as `Object.Field__c` under `CustomField` |
| Local deletion has no effect on the org | Regular deploys never delete | Use a destructive changes file (next chapters) |
Practice
Exercise
You delete AccountService.cls and its metadata file from force-app, keep AccountService listed in package.xml, and run sf project deploy start --manifest manifest/package.xml. What happens to the class in the org, and why?
Show answer
The class stays in the org. A normal deploy only creates or updates components that exist in the local project. The missing local file means the CLI has nothing to send for that member, and it never deletes anything. Removing the class requires a destructive changes file.
Exercise
You want to preview a manifest deploy, including test results, without saving anything to the org. Which two flags do you add?
Show answer
Add --dry-run to validate without saving, and --test-level (for example RunLocalTests) to run tests during the validation.