Sub topic 9
Creating your first scratch org
This chapter creates a scratch org from the definition file you wrote earlier. It covers the command, its main flags, what happens during creation, and the errors you are most likely to meet.
Prerequisites
This chapter assumes the following are already done:
- Dev Hub is enabled in your production, Developer Edition, or trial org, and you are a system administrator there.
- You authorized that org with
sf org login web --set-default-dev-hub --alias my-hub. - You generated a DX project with
sf project generate --name my-projectand are working inside it. config/project-scratch-def.jsonexists and contains valid JSON.
If any of these is missing, go back to the earlier chapter. Creation will fail without them.
What creation does
A scratch org is a temporary Salesforce org. The Dev Hub creates it from your definition file. The CLI sends the request to the Dev Hub, waits for the org to be built, then authorizes your machine to use the new org and stores the credentials locally.
Each creation counts against two Dev Hub limits: the number of active scratch orgs and the number created per day. A later chapter covers these in detail.
The create command
Run this from the project root:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --duration-days 7 --set-defaultThe flags do the following:
| Flag | Purpose |
|---|---|
| `--definition-file` | Path to the JSON file that describes the org. |
| `--alias` | A short name you use in later commands instead of the generated username. |
| `--duration-days` | Lifetime in days. The default is 7 and the maximum is 30. |
| `--set-default` | Makes this org the default target for commands that need an org. |
| `--target-dev-hub` | Names the Dev Hub to use. Needed only if you did not set a default Dev Hub. |
If you set a default Dev Hub during login, you can leave out --target-dev-hub. If you have several Dev Hubs and no default, the command fails until you pass this flag with an alias such as my-hub.
A successful run takes from under a minute to several minutes. It ends with output like this:
Your scratch org is ready.The CLI also reports the org ID and the generated username. The username looks like [email protected]. You rarely type it, because the alias replaces it.
Waiting for slow creation
Creation is normally synchronous: the CLI waits until the org is ready. You can limit the wait with --wait, which takes a number of minutes. If the wait expires, the org may still be building on the Dev Hub. The CLI prints a job ID, and you can resume with:
sf org resume scratch --job-id 2SRxx0000000000AAAReplace the sample ID with the one the CLI printed. Resuming is safer than starting a second creation, because a second request would use another slot from your limits.
Overriding values from the command line
Some definition values can be set at the command line, which is useful in scripts:
sf org create scratch --definition-file config/project-scratch-def.json --alias feature-a --duration-days 3 --edition developer --no-track-source--editionoverrides theeditionvalue in the file.--no-track-sourcecreates the org without source tracking. Tracking is on by default, and the next chapters rely on it fordeploy startandretrieve startto know what changed. Turn it off only if you know you will not need those change lists.
Use the file for settings you want to keep in version control, and flags for one-off differences.
Confirming the result
After creation, check that the org exists and that the alias is stored:
sf org listThe output lists your orgs in tables. Your Dev Hub appears with its alias, and the scratch org appears under a scratch org section. Look for these things:
- The alias
my-scratchin the Alias column. - A marker in the default column for the org you set with
--set-default. - An expiration date seven days from today.
To see one org in detail, use sf org display --target-org my-scratch. It shows the username, instance URL, org ID, and status. A later chapter covers opening and inspecting the org.
Common failures
Most creation errors come from the definition file, Dev Hub setup, or limits. Read the full message, because it usually names the cause.
| Symptom | Likely cause | Fix |
|---|---|---|
| `No default dev hub found` or a similar message | No default Dev Hub is set. | Re-run the login with `--set-default-dev-hub`, or pass `--target-dev-hub`. |
| `This org is not a Dev Hub` or an unauthorized error | Dev Hub is not enabled, or your user lacks permission. | Enable Dev Hub in Setup and check your permissions. |
| JSON parse error | A comma, quote, or brace is wrong in the definition file. | Validate the file in an editor, then retry. |
| Unknown feature or setting error | A feature name is misspelled, or the setting is not valid for the edition. | Check the spelling against the scratch org features and settings reference. |
| Limit error such as `LIMIT_EXCEEDED` | You reached the active or daily scratch org limit. | Delete unused scratch orgs, or wait until the daily count resets. |
| Duration error | `--duration-days` is outside 1 to 30. | Use a value in range. |
When the cause is not obvious, rerun the command with the --json flag. The structured output includes an error name and code that are easier to search for than a wrapped sentence in a terminal.
Good habits at creation time
- Pick an alias that describes the work, such as
feature-login. You may have several orgs at once, andmy-scratchtells you little. - Choose the shortest duration that fits the task. Short lifetimes keep your active count low.
- Keep the definition file in version control. Teammates then create identical orgs.
- Use
--set-defaultonly for the org you are actively using. Commands that omit--target-orgact on the default org, so a stale default can send a deploy to the wrong place.
Exercises
Exercise
You run sf org create scratch and the CLI stops waiting after the time given by --wait. The org might still be building. What should you do, and why not simply run the same command again?
Show answer
Resume the existing job with sf org resume scratch --job-id <id>, using the job ID the CLI printed. Running the create command again submits a second request, which uses another slot from the active and daily scratch org limits and can leave you with a duplicate org.
Exercise
Write the command that creates a scratch org from config/project-scratch-def.json, aliased feature-login, lasting 3 days, not set as the default, using the Dev Hub aliased my-hub.
Show answer
sf org create scratch --definition-file config/project-scratch-def.json --alias feature-login --duration-days 3 --target-dev-hub my-hub. Leaving out --set-default keeps the current default org unchanged.
Exercise
Creation fails with a message that the definition file cannot be parsed. Name two ways to find the problem.
Show answer
Open the file in an editor that validates JSON and look for a missing comma, an extra trailing comma, an unclosed brace, or single quotes instead of double quotes. You can also run the command with --json to get a structured error that points to the failure.