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 can run Salesforce CLI (sf) commands in a terminal. Here you will see which deploy and retrieve commands work with each layout, and what each one produces.
One command family, two kinds of input
The CLI uses the same two commands for both layouts: sf project deploy start and sf project retrieve start. The flags you pass decide which layout is involved.
- Source format uses
--source-dir,--metadata, or--manifest. The CLI reads your project, usingsfdx-project.jsonto find the package directories. - Metadata API format uses
--metadata-diron deploy and--target-metadata-diron retrieve. The CLI treats the folder as a ready-made package and does not readsfdx-project.json.
Deploying in source format
Point the CLI at a folder, a named component, or a manifest:
# Deploy everything under a folder
sf project deploy start --source-dir force-app
# Deploy one Apex class
sf project deploy start --metadata ApexClass:MyClass
# Deploy whatever package.xml lists
sf project deploy start --manifest manifest/package.xmlWhen you pass --source-dir or --metadata, the CLI converts the files to Metadata API format behind the scenes and sends them. You never see the converted files. The package.xml you pass to --manifest still sets which components are selected. The files themselves come from your package directories.
Deploying in Metadata API format
A Metadata API format folder already contains its own package.xml. Deploy the folder as is:
sf project deploy start --metadata-dir mdapi_output --wait 10--metadata-dir also accepts a .zip file. This is the route legacy tools and build systems use, because the folder is self-describing and no project file is needed. The trade-off is that source tracking does not apply. The CLI cannot report which components changed in the org or in your files.
Retrieving
In source format, a retrieve writes decomposed files into your package directories:
sf project retrieve start --manifest manifest/package.xml
sf project retrieve start --metadata CustomObject:AccountRetrieving CustomObject:Account produces Account.object-meta.xml and one file per field under fields/. It does not produce a single Account.object. A retrieve overwrites files that already exist locally, so commit or stash your edits first.
To get Metadata API format instead, give the retrieve a target folder:
sf project retrieve start --manifest manifest/package.xml \
--target-metadata-dir retrieved --unzipThe org returns a zip. The --unzip flag expands it into retrieved/, including the generated package.xml. Without it, you get the zip file.
Comparison
| Task | Source format | Metadata API format |
|---|---|---|
| Deploy | `--source-dir`, `--metadata`, `--manifest` | `--metadata-dir` |
| Retrieve | `--source-dir`, `--metadata`, `--manifest` | `--manifest` with `--target-metadata-dir` |
| Needs `sfdx-project.json` | Yes | No |
| Source tracking | Yes, in orgs that support it | No |
| Custom object on disk | Folder with one file per field | Single `.object` file |
Common failures
- "No source-backed components present" or nothing deployed. The path is not inside a package directory listed in
sfdx-project.json, or.forceignoreexcludes it. - Version mismatch. A retrieve or deploy uses
sourceApiVersionfromsfdx-project.jsonfor source format. It uses the<version>element inpackage.xmlfor Metadata API format. Check the right one. - A missing field after deploy. The field file sits outside the deployed folder, or it was not listed in the manifest. For example,
CustomObject:Accountlists the object, and the field files under it come with the object.
Exercise
You have a folder legacy_pkg with a package.xml and classes/ and objects/ subfolders, and no sfdx-project.json. Which command deploys it, and why does --source-dir legacy_pkg not work?
Show answer
Run sf project deploy start --metadata-dir legacy_pkg --wait 10. The folder is in Metadata API format, which --metadata-dir sends as is. --source-dir expects source format files in package directories, so it cannot read the monolithic layout.
Exercise
What flags retrieve the components in manifest/package.xml as an unzipped Metadata API format folder named out?
Show answer
sf project retrieve start --manifest manifest/package.xml --target-metadata-dir out --unzip