Sub topic 9
Previewing changes and running dry-run deploys
This chapter assumes you have a Salesforce DX project, an authorized org set as the default target, and that you can already run a basic sf project deploy start. It covers how to see what a deploy or retrieve would do before it changes anything, and how to run a deployment that the org checks but does not save.
Two kinds of preview
"Preview" covers two different checks. They answer different questions.
| Check | Command | Talks to the org? | Answers |
|---|---|---|---|
| Preview | `sf project deploy preview` | Yes, reads source tracking data | Which components would deploy, which would be deleted, and which conflict? |
| Dry-run deploy | `sf project deploy start --dry-run` | Yes, runs a real deployment that is rolled back | Would the deployment succeed, including compile errors and test results? |
A preview is fast and does not validate anything. It compares your local files with the org's state. A dry-run goes through the full deployment process on the server, but nothing is saved. Use the preview to see the scope of a change. Use the dry-run to find out whether the change will work.
Previewing with source tracking
Source tracking compares your local files with the org's metadata and records what changed on each side. It works only with orgs that support it, such as scratch orgs and sandboxes where you enabled it. Production orgs and sandboxes without tracking do not support it.
Run the preview against the default target org:
sf project deploy preview --target-org my-sandboxThe output groups components into tables. The exact wording depends on your CLI version, but you will see these ideas:
- Will deploy: local changes that a
deploy startwould send. - Will delete: components you deleted locally that would be removed from the org.
- Conflicts: components changed both locally and in the org since your last sync.
- Ignored: components that
.forceignoreexcludes.
You can also preview retrieves, which shows what sf project retrieve start would pull in:
sf project retrieve preview --target-org my-sandboxA conflict in the preview means a deploy would fail unless you overwrite the org's version. The conflict chapter covers how to resolve these. For now, treat a conflict in the preview as a prompt to decide which side is correct before you deploy.
You can narrow the preview to a subset with the same selectors that deploy uses, for example --source-dir or --metadata:
sf project deploy preview --source-dir force-app/main/default/classes --target-org my-sandboxWhat the preview does not tell you
The preview reports tracked differences only. It does not compile Apex, check that referenced fields exist, or run tests. A component can show as "Will deploy" and still fail on the server because it references something that is missing. That gap is why the dry-run exists.
Running a dry-run deploy
The --dry-run flag runs the full server-side deployment and then rolls it back instead of saving it:
sf project deploy start --source-dir force-app --target-org my-sandbox --dry-runThe command prints the same result tables as a normal deploy. Success means that the same command without --dry-run should succeed against an org in the same state. Failure lists the component, line, and message for each problem.
What a dry-run checks:
- Metadata validity: required fields, valid values, and missing references.
- Apex compilation of the classes and triggers in the deployment.
- Apex tests, according to the test level you request.
- Dependencies between components in the deployment and the org.
What a dry-run does not guarantee:
- The org can change between your dry-run and the real deploy. Another developer may deploy a conflicting change in between.
- A dry-run does not update source tracking. Nothing was saved, so the org's tracking data is unchanged.
- Running a dry-run that includes tests still consumes the time of those tests.
Combining a dry-run with a test level
The test level decides how much a dry-run proves. Without an explicit level, the default depends on the org. In a sandbox or scratch org, a deploy runs no tests unless your deployment requires them. In production, a deployment that includes Apex runs the local tests.
To rehearse a production deployment against a sandbox, set the level explicitly:
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --dry-run --test-level RunLocalTestsThis runs every local Apex test, which excludes tests from managed packages. If the dry-run passes with this level, you have good evidence that the same deployment passes the production test requirement, though production data and settings can still cause differences.
Reading a failed dry-run
When a dry-run fails, the result separates deployment problems from test problems. Read them in this order:
- Component failures. These show the file, line number, and error. Fix them first, because test results are unreliable when components fail to compile.
- Test failures. These show the test class, method, and the assertion message or stack trace.
- Code coverage warnings. These show classes below the required coverage percentage.
After you fix a problem, run the dry-run again. Because nothing was saved, each attempt starts from the same org state, and you do not need to clean anything up.
Dry-run, validate, and quick deploy
A dry-run and a validation both check a deployment without saving it. They differ in what you can do afterward.
| Feature | `deploy start --dry-run` | `deploy validate` |
|---|---|---|
| Saves changes | No | No |
| Can be quick-deployed later | No | Yes, if tests passed |
| Typical use | Fast check during development | Pre-release check for production |
Use --dry-run while you iterate in a sandbox. Use sf project deploy validate when you want to deploy the same validated job to production without running the tests again. The next chapters cover test levels and quick deploys in detail.
A diagram of the workflow
Common mistakes
- Expecting preview to catch compile errors. It does not compile anything. Run a dry-run.
- Running a preview on an org without source tracking. The command reports an error or shows no tracked changes. Use a dry-run, or deploy with an explicit
--source-diror--manifest. - Assuming a passing dry-run covers a later production deploy. The org state may change, and the test level may differ. Match the test level and deploy soon after.
- Forgetting
--target-org. Without it the command uses the default target org, which may not be the org you meant to check. Confirm the default withsf config get target-org. - Reading a dry-run result as a record of what was deployed. Nothing was deployed, so there is nothing to find in the org afterward.
Checking a dry-run you stopped watching
A large dry-run with many tests can run longer than the command waits. If the command stops waiting, the job keeps running on the server. Check on it with the report and resume commands:
sf project deploy report --use-most-recent
sf project deploy resume --use-most-recentResuming a dry-run keeps the same behavior. When it finishes, the result still shows that nothing was saved.
Exercises
Exercise
A teammate says, "The preview shows my Apex class as 'Will deploy', so the deploy will succeed." What is wrong with that claim, and which command would test it?
Show answer
The preview only compares tracked local and org state. It does not compile Apex, check references, or run tests, so the class can still fail on the server. Run sf project deploy start --dry-run with the manifest or source directory, and add --test-level RunLocalTests if tests matter.
Exercise
You run sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --dry-run and it passes. Later you check the org and the changes are missing. Is something wrong?
Show answer
No. A dry-run runs the full deployment and rolls it back, so it saves nothing. Run the same command without --dry-run to apply the changes.
Exercise
You want to check a release for production now, and deploy it tomorrow without rerunning tests. Should you use --dry-run or sf project deploy validate, and why?
Show answer
Use sf project deploy validate with --test-level RunLocalTests. A dry-run cannot be quick-deployed, while a validated job that passed its tests can be quick-deployed with sf project deploy quick --job-id <id>.