Sub topic 5
Retrieving and Deploying with --manifest
This chapter covers how to pass a package.xml manifest to the Salesforce CLI to retrieve and deploy metadata. It assumes you know the structure of package.xml (types, members, name, version) and the member naming rules from earlier chapters. It also assumes the following:
- You have
sfinstalled. - You have authorized a sandbox with the alias
my-sandbox. - You work in a Salesforce DX project with a
force-appsource directory.
The destructive changes chapters that follow reuse this exact deploy command, so it helps to understand it well first.
What --manifest does
The --manifest flag (short form -x) points a command at a package.xml file. The CLI reads the file and turns each types block into a list of components. It then acts on that list. The command is the same for retrieve and deploy, but the direction differs:
sf project retrieve start --manifestasks the org for the listed components and writes them into your local project.sf project deploy start --manifestreads the listed components from your local project and sends them to the org.
The manifest is a selection, not a container. It holds no metadata itself, only names. The files that carry the content are the source files in your project, or the files the org returns.
A manifest to work with
The examples below use this manifest, saved as manifest/package.xml:
<?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>The version element sets the Metadata API version used for the operation. Use the same value across your team's manifests, so a retrieve on one machine produces the same file contents as a retrieve on another.
If you do not yet have a manifest, generate one from the project source:
sf project generate manifest \
--source-dir force-app \
--name manifest/package.xmlYou can also build a manifest from a list of metadata names with --metadata ApexClass:AccountService. Review the generated file before using it. It lists everything under force-app, which may be more than you want to deploy.
Retrieving with a manifest
Run a retrieve against the sandbox:
sf project retrieve start \
--manifest manifest/package.xml \
--target-org my-sandboxThe CLI writes each component into the package directory that sfdx-project.json marks as the default (usually force-app). It converts the result to source format. For example, one custom field becomes one .field-meta.xml file under objects/Account/fields/.
Retrieve has these behaviors:
- It overwrites local files. If the org version differs from your local file, the org version wins. Commit or stash your edits before you retrieve.
- A missing member produces a warning, not a clean result. If the org has no
AccountServiceTest, the CLI reports it as not found. Check the output table. A misspelled member means a component you expected to see is absent from your project. - A retrieve can return more files than you listed. Retrieving a custom object, for example, can bring in its fields, depending on how you list it. This is why precise member naming matters.
If you want the raw Metadata API format, the zip that package.xml originally referred to, use the --target-metadata-dir flag. Most DX projects do not need it. It is mainly useful for tools that still work with the older format.
Wildcards in retrieve
The wildcard manifest from the earlier chapter selects everything of a type:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>ApexClass</name>
</types>
<version>62.0</version>
</Package>Used with retrieve start, this pulls every Apex class in the org, including classes from installed managed packages that the org lets you see. That is useful for a first sync. It is a poor fit for a routine deploy, because you rarely want every class to move at once.
Deploying with a manifest
Deploy the same selection to the sandbox:
sf project deploy start \
--manifest manifest/package.xml \
--target-org my-sandboxThe CLI looks up each listed component in your local project, packs them into a deployment, and sends it to the org. By default it waits for the result and prints a table of components deployed, plus any test results.
Three points set a manifest deploy apart from other ways of deploying:
- The manifest decides what is sent, not what is in the folder. If
force-apphas fifty classes and the manifest lists two, only two go to the org. - Every listed component must exist locally. If the manifest names
AccountServiceand there is no matching source file in any package directory, the CLI cannot send it. The result is an error or a warning, depending on the type. A manifest member with no local source is the most common cause of "why did my deploy do nothing". - A deploy is all or nothing by default. If one component fails, none of them are saved. The org rolls the whole deployment back.
Choosing your selection method
deploy start accepts --manifest, --source-dir, and --metadata, and each fits a different situation. Pick one of these per command.
| Flag | You provide | Best for |
|---|---|---|
| `--manifest` | A `package.xml` file | Repeatable deploys, release packages, anything that goes into version control |
| `--source-dir` | A folder or file path | Quick deploys of what you just edited |
| `--metadata` | Type and name, such as `ApexClass:AccountService` | One-off deploys of a few named components |
For deletions, the manifest route matters most. The destructive changes flags you meet in later chapters work alongside --manifest, and a manifest file is what you keep in source control as a record of what you shipped.
Validating before you deploy
Add --dry-run to run the full deployment check without saving anything to the org:
sf project deploy start \
--manifest manifest/package.xml \
--dry-run \
--target-org my-sandboxThe org runs the same compile and dependency checks as a real deployment, and it reports the same errors. Nothing is committed. Make this a habit. It costs you one extra command, and it catches missing dependencies, bad member names, and failing tests while the change is still reversible.
Test levels
The --test-level flag controls which Apex tests run during the deploy:
sf project deploy start \
--manifest manifest/package.xml \
--test-level RunSpecifiedTests \
--tests AccountServiceTest \
--target-org my-sandboxThe common values are:
NoTestRun: no tests run. Allowed only in sandboxes and scratch orgs.RunSpecifiedTests: runs the test classes you name with--tests.RunLocalTests: runs all tests in your org except those from managed packages.RunAllTestsInOrg: runs every test, including managed packages.
Production deployments that include Apex run local tests by default, and the code coverage requirement applies. Plan for the run time when you schedule a production deploy.
Comparing what the manifest says with what is in the org
Before you deploy, you can preview what would change. The command sf project deploy preview lists what a deploy would send, and flags conflicts with changes made directly in the org:
sf project deploy preview \
--manifest manifest/package.xml \
--target-org my-sandboxThis works with source-tracked orgs, such as scratch orgs and tracking-enabled sandboxes. For a sandbox without tracking, the preview cannot tell you about conflicts. In that case, retrieve first and compare the results in version control.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The deploy reports no components, or finishes instantly | Manifest members have no matching local source | Check spelling and case, and confirm the files are in a package directory listed in `sfdx-project.json` |
| A custom field is not found | The member is written `Region__c` instead of `Account.Region__c` | Use the `Object.FieldName` form |
| Retrieve returns far more than expected | A wildcard or a whole-object member | List specific members, or generate the manifest from a narrower `--source-dir` |
| The deploy fails on a version error | `version` is higher than the org supports | Lower the version, or use a more recent org |
| Retrieve overwrote your edits | Local changes were not committed | Restore from version control, and commit before every retrieve |
Why this matters for deletion
A manifest deploy only adds or updates components. Removing a component from package.xml, or deleting its local file, does not delete it from the org. The org keeps the component until a destructive changes file tells it to remove it. The next chapters build on the command you just learned and add that second file to the same deployment.
Exercise
You run sf project deploy start --manifest manifest/package.xml --target-org my-sandbox. The manifest lists the Apex class OldHelper, but you deleted OldHelper.cls from your project last week. What do you expect to happen to OldHelper in the org, and what would you do to check the result before running the command for real?
Show answer
Nothing is deleted. A manifest deploy sends only components that exist in the local project, and OldHelper has no local source to send, so the CLI reports it as missing and the org copy stays as it was. To check before running it for real, add --dry-run and read the result table. To actually remove the class from the org, you need a destructive changes file, which a later chapter covers.
Exercise
Why is --manifest a better choice than --source-dir for a release that you want to reproduce exactly next week?
Show answer
A manifest is a file that lists the exact components and API version, so you can commit it to version control and run the same selection again. A --source-dir path selects whatever happens to be in that folder at the time, so the deployed set changes whenever someone adds or edits files there.