Sub topic 9
Deploying and Retrieving in Each Format
This chapter assumes you know what a package.xml manifest is and how the two layouts differ. It also assumes you have a Salesforce DX project and the Salesforce CLI (sf) installed, and that you are comfortable with Git. It covers which commands move metadata between your disk and an org, what each command reads and writes, and what goes wrong when the format and the command don't match.
One API, two on-disk layouts
Deploys and retrieves always go through the Metadata API. The org only understands the Metadata API format. Source format is a convenience layer in the CLI.
- Deploying source format: the CLI converts your source files into Metadata API format in a temporary location, zips them, and sends the zip.
- Retrieving into a project: the CLI receives Metadata API format and converts it into source format, writing the files into your package directories.
- Using Metadata API format directly: no conversion happens. You point the CLI at a directory or zip that is already in Metadata API layout.
This explains most behavior in the rest of the chapter. Source-format commands select components by path, manifest, or metadata name. Metadata API-format commands work on a whole directory or zip.
Deploying source format
Source-format deploys use sf project deploy start. Run it from inside the project, because the CLI reads sfdx-project.json to find your package directories. There are three common ways to say what to deploy.
# 1. By path: everything under a folder or a single file
sf project deploy start --source-dir force-app/main/default/classes
# 2. By metadata name: type and optional member
sf project deploy start --metadata ApexClass:MyClass --metadata CustomObject:Account
# 3. By manifest: the package.xml lists what to include
sf project deploy start --manifest manifest/package.xml --wait 10The three options select different things:
--source-dirselects files on disk. You can repeat it.--metadataselects components by type and name. The CLI looks them up in your package directories.--manifestselects components by thepackage.xmlcontents. The manifest chooses which local components to send. It does not change where the files live.
In every case, only components that exist in your local project can be sent. If the manifest names MyClass and no such file exists in a package directory, the CLI reports it as missing and does not create it.
If the target org has source tracking (scratch orgs and sandboxes that enable it), you can run the command with no selection flag. The CLI deploys the local changes it detects. To see what would happen first, run sf project deploy preview.
Checking before you commit to a deploy
A dry run sends the payload to the org and runs validation, but it commits nothing.
sf project deploy start --manifest manifest/package.xml --dry-run --test-level RunLocalTests --wait 30Deploys to production that include Apex require tests. --test-level controls which tests run. Common values are NoTestRun, RunSpecifiedTests, RunLocalTests, and RunAllTestsInOrg. A dry run with RunLocalTests is a good gate before a release.
Retrieving into source format
Retrieve is the reverse. sf project retrieve start takes the same kinds of selectors.
# By manifest
sf project retrieve start --manifest manifest/package.xml
# By metadata name
sf project retrieve start --metadata CustomObject:Account
# By path: refresh the files that already exist locally
sf project retrieve start --source-dir force-app/main/default/objectsThe CLI converts what it receives into source format. A retrieved Account custom object arrives as one monolithic payload and is written as Account.object-meta.xml plus fields/, listViews/, and other subfolders. Fields that already exist are overwritten in place. Fields that were deleted in the org are not removed from your disk by a plain retrieve, so a stale local file can survive.
Where the files land depends on your package directories:
- A component that already exists locally is written back to the file it came from.
- A new component goes to the default package directory (the one marked
"default": trueinsfdx-project.json). --output-dir(short form-r) redirects new files to another folder so you can compare without touching your working tree.
You can also generate a manifest from the project or the org instead of writing it by hand:
# From local source
sf project generate manifest --source-dir force-app --name manifest/package
# From the org: everything of the listed types
sf project generate manifest --from-org myOrg --metadata ApexClass --metadata CustomObjectDeploying Metadata API format
When you have a directory in Metadata API layout (for example, the output of sf project convert source), you deploy it with --metadata-dir. The directory must contain a package.xml at its root.
sf project deploy start --metadata-dir mdapi_output --wait 10--metadata-dir also accepts a zip file. Two points matter here:
- The CLI does not look at your
sfdx-project.jsonfor this deploy. No package directories are involved, so the command can run anywhere. - The manifest is the single source of truth. Anything in the directory but not in
package.xmlis not deployed. Anything listed inpackage.xmlbut missing from the directory is a deploy error.
If your directory holds exactly one package rather than a folder of packages, the CLI has a --single-package flag to say so. Use it when you get an error about not finding package.xml even though the file is present.
Retrieving Metadata API format
To pull metadata as Metadata API format, use --target-metadata-dir. The CLI does no conversion in this case.
sf project retrieve start \
--manifest manifest/package.xml \
--target-metadata-dir mdapi_retrieve \
--unzip \
--wait 10Without --unzip, the CLI leaves a zip file in the target directory. With it, you get the familiar package.xml, classes/, and objects/Account.object layout. A retrieve in this mode needs a manifest. It cannot select by path, because there is no local source to look up.
This is the mode to use for legacy tooling, for archiving a snapshot of an org, or for loading metadata into tools that expect the older layout.
Side by side
| Task | Source format | Metadata API format |
|---|---|---|
| Deploy | `sf project deploy start --source-dir`, `--metadata`, or `--manifest` | `sf project deploy start --metadata-dir` |
| Retrieve | `sf project retrieve start --manifest`, `--metadata`, or `--source-dir` | `sf project retrieve start --manifest ... --target-metadata-dir` |
| Needs `sfdx-project.json` | Yes | No |
| Needs `package.xml` | Only with `--manifest` | Always |
| Select by file path | Yes | No |
| Conversion by the CLI | Yes, both directions | None |
| Source tracking | Yes, in tracked orgs | No |
| Diff-friendly output | Yes, decomposed files | No, large monolithic files |
Failure modes
Most deploy and retrieve problems come from a mismatch between the command and the layout.
- Passing source format to
--metadata-dir. The CLI looks forpackage.xmlat the top of the directory. Aforce-appfolder has none, so the deploy fails. Convert first, or use--source-dir. - Passing Metadata API format to
--source-dir. The CLI treats the files by their suffixes. A file likeAccount.objectis not a recognized source-format component, so it is skipped or rejected. Convert withsf project convert mdapi. - Manifest members that don't exist locally. A source-format deploy by manifest reports each missing member. The remedy is to retrieve it first or fix the name.
- Wildcards.
<members>*</members>in a manifest selects every component of that type. Retrieving with a wildcard can bring in many files. Deploying with it sends everything local of that type, which may include components you did not mean to touch. - API version mismatch. The
<version>inpackage.xmlandsourceApiVersioninsfdx-project.jsoncan differ. The manifest version applies to the retrieve or deploy request, and a component may then be saved at a different version than your team expects. Keep the two aligned. - Stale local files after a retrieve. As noted above, a retrieve does not delete files for components removed in the org. Review
git statusafter a retrieve and remove them yourself.
A worked round trip
This sequence retrieves two components into the project, then ships the same components as Metadata API format.
# 1. Retrieve into source format
sf project retrieve start --metadata ApexClass:MyClass --metadata CustomObject:Account
# 2. Convert the source for those components into Metadata API format
sf project convert source --root-dir force-app --output-dir mdapi_output
# 3. Validate the converted directory against the org without saving
sf project deploy start --metadata-dir mdapi_output --dry-run --wait 10If step 3 succeeds, the converted directory is well formed, and you can run the same command without --dry-run.
Exercise
You have a folder named mdapi_output that you created with sf project convert source. A teammate runs sf project deploy start --source-dir mdapi_output and the deploy does not do what they expect. What is the likely cause, and what is the correct command?
Show answer
--source-dir expects source format, but mdapi_output is in Metadata API format (it has package.xml and files such as Account.object). The CLI cannot match those files to source components. The correct command is sf project deploy start --metadata-dir mdapi_output --wait 10.
Exercise
You need a snapshot of all Apex classes in an org, in the layout that a legacy tool expects, and you want the files unzipped. Which command and flags do you use, and what must you provide?
Show answer
Use sf project retrieve start --manifest package.xml --target-metadata-dir snapshot --unzip. You must provide a manifest, for example one listing ApexClass with * as the member, because a Metadata API-format retrieve selects by manifest and not by local file path.