Sub topic 9
Create a scratch org
A scratch org is a temporary, disposable Salesforce org that you create from a configuration file. This chapter assumes you have installed the CLI, authorized an org, and generated a Salesforce DX project. It also assumes you know what an org's edition and metadata are.
Why use a scratch org
A scratch org starts from a definition you control, so every developer gets the same clean environment. You can deploy your project to it, run tests, and then throw it away. This is safer than experimenting in a sandbox or production org that other people share.
Scratch orgs have two limits. They expire, with a default of 7 days and a maximum of 30. They also depend on a Dev Hub.
The Dev Hub
The Dev Hub is a regular org with the Dev Hub feature enabled. It owns and tracks your scratch orgs. You can enable the feature in Setup, under Development, then Dev Hub. Developer Edition orgs and production orgs can serve as a Dev Hub.
Authorize the Dev Hub and mark it as the default Dev Hub:
sf org login web --alias devhub --set-default-dev-hubThe CLI needs a default Dev Hub so it knows where to create scratch orgs. If you have several, pass --target-dev-hub on the create command instead.
The definition file
The definition file describes the org you want. Save a minimal one as config/project-scratch-def.json in your project:
{
"orgName": "Demo scratch org",
"edition": "Developer"
}edition selects the base feature set. orgName sets the company name shown in the new org. Larger projects add features and settings keys to enable options such as multi-currency or custom Lightning settings.
Create the org
Run the create command from the project directory:
sf org create scratch --definition-file config/project-scratch-def.json --alias myscratch --duration-days 7 --set-defaultThe flags work like this:
--definition-filepoints to the JSON file above.--aliasgives the new org a short name you can use in later commands.--duration-dayssets how long the org lives before it is deleted.--set-defaultmakes it the default target org for this project.
The command contacts the Dev Hub, provisions the org, and stores its login information locally. Creation usually takes a minute or two.
Check and use the new org
Confirm that the org exists and is the default:
sf org list
sf org display
sf org opensf org list shows scratch orgs with their expiration dates. sf org open opens the org in your browser without a password. Because you used --set-default, commands such as sf project deploy start now target this org without a --target-org flag.
Delete an org you no longer need
Each Dev Hub limits how many active scratch orgs you can have, so clean up when you finish:
sf org delete scratch --target-org myscratchThe CLI asks for confirmation before deleting. Deleting does not remove your local project files. It removes only the org.
Common errors
- "No default Dev Hub" or a similar message. You have not authorized a Dev Hub with
--set-default-dev-hub, or the org you authorized does not have Dev Hub enabled. - A limit error on creation. Your Dev Hub has reached its active scratch org limit. List your orgs and delete ones you no longer use.
- A definition file error. Check the JSON for trailing commas, and check that the file path is relative to where you ran the command.
Exercise
Exercise
You run sf org create scratch with a valid definition file and an alias, but you leave out --set-default. The org is created. What is different when you next run sf project deploy start --source-dir force-app, and how do you fix it without creating the org again?
Show answer
The new org is not the default target org, so the deploy goes to whatever org was already the default, or fails if none is set. Either add --target-org myscratch to the deploy command, or run sf config set target-org myscratch to make the scratch org the default.