Sub topic 9
Creating your first scratch org
This chapter assumes you have already enabled Dev Hub and authorized it from the CLI with the alias my-hub. It also assumes you have generated a Salesforce DX project that contains config/project-scratch-def.json. Run every command from the project root.
The create command
One command creates the org:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --duration-days 7 --set-defaultEach flag controls one part of the result:
| Flag | Purpose |
|---|---|
| `--definition-file` | Path to the JSON file that sets edition, features, and settings. |
| `--alias` | A short name you use in later commands instead of the generated username. |
| `--duration-days` | Lifetime in days, from 1 to 30. The default is 7. |
| `--set-default` | Makes this org the default target for later commands in this project. |
If you have not set a default Dev Hub, add --target-dev-hub my-hub. Otherwise the CLI cannot tell which Dev Hub should own the org.
What happens during creation
The CLI does not build the org itself. It sends your definition to the Dev Hub, which provisions the org and returns login details. The CLI then stores the authorization locally.
Creation usually takes a few minutes. The command waits and prints the org ID and username when it finishes.
Confirm it worked
List the orgs the CLI knows about:
sf org listYour scratch org appears under the scratch orgs section with the alias my-scratch. A marker shows that it is the default. The expiration date is the creation date plus your duration.
Common errors
- No Dev Hub found. The CLI has no default Dev Hub. Pass
--target-dev-hub my-hub, or authorize again with--set-default-dev-hub. - Invalid feature or setting. The Dev Hub rejects names it does not recognize. A typo in
featuresor a misspelled key undersettingsfails the whole request. The error message names the offending entry, so fix that entry in the JSON and run the command again. - Limit reached. Each Dev Hub caps both the scratch orgs that are active at once and the number created per day. Delete unused orgs, or wait for the daily count to reset.
- Duration out of range. Values below 1 or above 30 are refused.
Check your understanding
Exercise
You run the create command with a definition file that contains a misspelled feature name. The command fails. Which of the Dev Hub's scratch org limits have you used up, and what should you do?
Show answer
None. A misspelled feature is a definition file error, so the Dev Hub rejects the request before provisioning anything. Read the error message to find the invalid entry, correct it in the JSON file, and run the command again.