Sub topic 8
Deploy and retrieve metadata
This chapter covers moving metadata between your local DX project and an org. It assumes you can run sf commands, have authorized an org, and have set a default target org. It also assumes you have a project from sf project generate and know what metadata is, such as Apex classes, custom objects, and layouts.
What deploy and retrieve do
Metadata is the configuration and code that defines an org. In a DX project it lives as files under a package directory, usually force-app. Two commands move it between the project and an org:
sf project deploy startsends local files to the org.sf project retrieve startpulls the org's version of the metadata into local files.
Both commands use the Metadata API. They do not copy data records. For records, use the data commands from a later chapter.
Deploy a directory
The simplest form deploys everything under a source directory:
sf project deploy start --source-dir force-app
sf project retrieve start --source-dir force-appBoth commands act on the default target org. To use a different org, add --target-org (short form -o) with an alias or username:
sf project deploy start --source-dir force-app --target-org myorgYou can pass --source-dir more than once, or give it a path to a single file or folder:
sf project deploy start \
--source-dir force-app/main/default/classes \
--source-dir force-app/main/default/triggersThe CLI converts your source format into the Metadata API format before sending it. This is why a source directory works even though the API itself expects a different layout.
Try it with a small change
Create an Apex class, deploy it, then retrieve it back.
sf apex generate class --name HelloWorld --output-dir force-app/main/default/classes
sf project deploy start --source-dir force-app/main/default/classesThe generate command creates HelloWorld.cls and HelloWorld.cls-meta.xml. Both files are needed. The -meta.xml file holds the API version and status, and a deploy that is missing it fails.
When the deploy finishes, the CLI prints a table of the components it deployed, with the state of each one (for example, Created or Changed). If a component fails, the table lists its file, line, and the error message.
Three ways to say what to deploy
You can select metadata by directory, by metadata name, or by manifest. Only one of these selectors is needed per command.
| Selector | Flag | Best for |
|---|---|---|
| Source directory | `--source-dir` (`-d`) | Deploying files or folders you changed locally |
| Metadata name | `--metadata` (`-m`) | Naming a type or a specific component |
| Manifest | `--manifest` (`-x`) | Repeatable, reviewed lists of components |
By metadata name
The --metadata flag takes a type, or a type and a component name separated by a colon. Quote values that contain wildcards so your shell does not expand them.
sf project retrieve start --metadata ApexClass
sf project retrieve start --metadata ApexClass:HelloWorld
sf project retrieve start --metadata "ApexClass:Hello*" --metadata CustomObject:AccountRetrieving by name is the usual way to pull a component that exists in the org but not yet in your project.
By manifest
A manifest is a package.xml file that lists the components. It is the same format the Metadata API uses, and it is handy because you can review it in version control. This example covers all Apex classes and one custom object:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>ApexClass</name>
</types>
<types>
<members>Account</members>
<name>CustomObject</name>
</types>
<version>60.0</version>
</Package>Save it as manifest/package.xml, then use it for either direction:
sf project deploy start --manifest manifest/package.xml
sf project retrieve start --manifest manifest/package.xmlYou can also generate a manifest from existing source with sf project generate manifest --source-dir force-app --output-dir manifest.
Check before you deploy
Two features help you avoid surprises.
Preview
sf project deploy preview lists what a deploy would send, which conflicts exist, and what would be deleted. sf project retrieve preview does the same for retrieve. Neither command changes the org or your files.
sf project deploy preview --source-dir force-appDry run
--dry-run sends the deploy to the org and runs validation, including tests if you request them. Nothing is saved. This is the "validate only" behavior, and it is the safest way to check a deployment to production.
sf project deploy start --source-dir force-app --dry-run --test-level RunLocalTestsA preview is local and fast. A dry run exercises the real server-side checks. Use the preview to see the scope, then the dry run to see whether it will succeed.
Source tracking and conflicts
Scratch orgs and some sandboxes can track source changes. The CLI keeps a record of what changed locally and what changed in the org. With tracking on, sf project deploy start with no selector deploys only your local changes, and sf project retrieve start with no selector pulls only the org's changes.
Without tracking, as in production or a Developer Edition org, there is no such record. You must name what to deploy or retrieve with --source-dir, --metadata, or --manifest. The commands fail if you give none.
When both sides changed the same component since the last sync, the CLI reports a conflict and stops. You have two options:
- Resolve the difference by hand, then retry.
- Add
--ignore-conflicts(-c) to let your side win. For deploy, local source overwrites the org. For retrieve, the org's version overwrites your files.
Control what is included
A .forceignore file at the project root works like .gitignore. Patterns in it are skipped by deploy and retrieve in both directions. Use it for files that should never travel, such as profiles you manage elsewhere or test scaffolding.
**/jsconfig.json
**/.eslintrc.json
force-app/main/default/profiles/**Be careful with ignore patterns. If a deploy "succeeds" but a component never appears in the org, check .forceignore first.
Tests and test levels
Deploying to a production org runs Apex tests. Deploying to sandboxes and scratch orgs does not require them. The --test-level flag sets the behavior:
| Value | Meaning |
|---|---|
| `NoTestRun` | No tests run. Not allowed for production deploys that include Apex. |
| `RunSpecifiedTests` | Runs only the tests named with `--tests`. |
| `RunLocalTests` | Runs all tests in your org except those from managed packages. |
| `RunAllTestsInOrg` | Runs every test, including managed packages. |
sf project deploy start --source-dir force-app \
--test-level RunSpecifiedTests --tests HelloWorldTestProduction deploys also enforce code coverage. Check the current requirements for your org before relying on a single named test.
Long-running deploys
By default the command waits for the deploy to finish, up to a time limit set by --wait in minutes. Large deploys can outlast that limit. When the wait ends, the deploy keeps running in the org, and the CLI prints a job ID. You can then check on it:
sf project deploy start --source-dir force-app --wait 30
sf project deploy report --use-most-recent
sf project deploy resume --use-most-recent
sf project deploy cancel --use-most-recentreportshows the current status without waiting.resumereattaches to the job and waits for it.cancelstops a running deploy.
Without --use-most-recent, pass --job-id with the ID that the deploy command printed.
Common errors
- No source-tracking support, or "nothing to deploy". The org does not track source, or you gave no selector. Add
--source-dir,--metadata, or--manifest. - Conflicts detected. The org and your project both changed. Review with the preview commands, then resolve or use
--ignore-conflicts. - Missing
-meta.xml. Every Apex class and trigger needs its companion file in the same folder. - Dependency errors. A component refers to something the org lacks, such as a custom field used in a layout. Deploy the dependency in the same command, or include its parent directory.
- Wrong org. The command used the default org when you meant another. Run
sf org displayfirst, or pass--target-orgevery time in scripts. - Test failures. Production deploys fail when tests or coverage fall short. Use
--dry-runwith the right--test-levelto find out before the real deploy.
Recommended workflow
- Run
sf org displayto confirm the target org. - Run
sf project deploy previewto see the scope and conflicts. - Run a
--dry-rundeploy when the target is a shared or production org. - Run the real deploy and read the component table.
- Retrieve afterward if you changed things in the org's UI, and commit the result.
Exercise
You run sf project deploy start with no other flags against a Developer Edition org, and the command fails. Why does this happen, and what are two ways to fix it? Also name the flag that would let you check a deployment to production without saving anything.
Show answer
Developer Edition orgs do not support source tracking, so the CLI has no record of local changes and needs you to say what to deploy. Fix it by adding a selector: --source-dir force-app, or --metadata ApexClass:HelloWorld, or --manifest manifest/package.xml. To validate without saving, use --dry-run, typically with --test-level RunLocalTests.
Exercise
Write one command that retrieves every Apex class whose name starts with Account, plus the Contact custom object, from the org with alias myorg.
Show answer
sf project retrieve start --metadata "ApexClass:Account*" --metadata CustomObject:Contact --target-org myorg