Sub topic 2
Sandboxes versus scratch orgs
This chapter assumes you know what a sandbox is and why teams use one. It also assumes you can tell metadata (configuration and code) from data (records), and that you have seen the Salesforce CLI. Scratch orgs are the other kind of non-production org you will meet, and people often confuse the two. This chapter explains how they differ so you can pick the right one.
The core difference
A sandbox is a copy of a specific org, usually production. It carries over that org's metadata, and depending on the type it can also carry some or all of its data. It lives until you delete it, and you bring it up to date by refreshing it.
A scratch org is a disposable org built from a definition file. It is created from a Dev Hub, which is a production or Developer Edition org that has Dev Hub enabled. It starts empty except for the features and settings you request. It expires automatically, after 7 days by default and up to 30 days.
In short, a sandbox answers "what does my real org look like?" and a scratch org answers "what should a clean org with these features look like?"
Where each one comes from
| Aspect | Sandbox | Scratch org |
|---|---|---|
| Source of its contents | A copy of production (or of another sandbox) | A definition file plus metadata you push |
| Created from | Production org, or another sandbox as a clone | A Dev Hub org |
| Production data | Depends on type: none, a sample, or all | None. You load test data yourself |
| Lifetime | Until deleted | 1 to 30 days, then it expires |
| Refresh | Yes, on a type-specific interval | No. You create a new one |
| Typical creation time | Minutes to hours or longer, growing with size | Usually minutes |
| Best for | Realistic testing and staging | Isolated, repeatable development |
How scratch orgs are defined
A scratch org definition file is JSON that lives in your project, normally in config/project-scratch-def.json. It sets the edition, enabled features, and org settings.
{
"orgName": "Feature Dev Org",
"edition": "Developer",
"features": ["EnableSetPasswordInApi"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}You create the org with the CLI. The --target-dev-hub flag names the Dev Hub that owns it, and --duration-days sets the expiry.
sf org create scratch --definition-file config/project-scratch-def.json --alias feature1 --duration-days 7 --target-dev-hub devhubCompare this with the sandbox definition file from the CLI example later in the course. A sandbox definition names the sandbox and sets a license type. It does not describe what is inside, because the contents come from the source org.
sf org create sandbox --definition-file config/sandbox-def.json --alias dev1 --target-org prodHere --target-org points at the org you are copying from. A scratch org command points at a Dev Hub, which only owns the org. It is not copied.
What lives where: the source of truth
The most important practical difference is where the truth about your configuration lives.
- Sandbox workflow: The sandbox is a copy of production, so production holds the truth. You build changes in the sandbox, then deploy them back to production through a change set or a deployment tool.
- Scratch org workflow: Your version control repository holds the truth. The scratch org is a throwaway build target. You push source into it, test it, pull back any changes, commit them, and delete the org.
This is why scratch orgs belong to source-driven development. Because you can recreate one from the repository at any time, losing one costs nothing. A sandbox is different. It often holds work in progress that exists nowhere else, which is why the refresh chapters spend time on preparing to protect that work.
flowchart TDThe diagram below shows the two flows side by side.
Data and realism
A scratch org has no production data. You create records with a data plan, a script, or Apex. That keeps tests predictable, but it means a scratch org will not reveal problems that only appear with real volumes, odd records, or years of accumulated configuration.
A sandbox can reveal those problems. A Partial Copy sandbox brings a sample of data, and a Full sandbox brings everything. Even a Developer sandbox brings all of production's metadata, including old validation rules, triggers, flows, and installed packages that interact with your change.
Realism has a cost. A sandbox inherits production's mess, including conflicting automation and unmanaged customizations. A scratch org starts clean, so a failure there points to your own change.
Limits and ownership
Scratch orgs are limited by the Dev Hub. It caps both the number of active scratch orgs and the number you can create per day, and the caps depend on the Dev Hub's edition. Check the current numbers in the Dev Hub's Limits page, because they change between releases.
Sandboxes are limited by the sandbox licenses your edition includes. Each type has a license count, a storage cap, and a refresh interval. You cannot create more sandboxes of a type than you have licenses for. The next chapters cover these limits.
A scratch org also cannot be refreshed. If you want a fresh one, create another from the same definition. A sandbox cannot be recreated that cheaply, because its refresh depends on production and on an interval you cannot shorten.
Choosing between them
Use a scratch org when you want fast, repeatable, isolated development, especially for packages or when your team works from version control. Use a sandbox when you need production's real configuration, real data shape, or a place for QA, user acceptance testing, integration testing, or training.
Many teams use both. Developers build features in scratch orgs, then merge into a shared integration sandbox where the work meets production's real metadata. A Full sandbox then serves as staging before release.
Developer and Developer Pro sandboxes can also use source tracking. This gives you a push and pull workflow similar to a scratch org, but against a copy of your real org.
Common mistakes
- Treating a scratch org as a staging environment. It has no production metadata or data, so a pass there does not prove the change works in production.
- Waiting for a scratch org to refresh. There is no refresh. Create a new org from the definition file.
- Forgetting the Dev Hub. Scratch orgs need a Dev Hub org and an authorization to it. Sandboxes need only the source org.
- Letting a definition file drift. If someone adds a setting by hand in a scratch org and never records it, the next scratch org will not match. Capture settings in the definition file.
- Using one environment for everything. A shared sandbox for development, QA, and demos leads to conflicts. Separate them.
Exercise
Your team keeps all metadata in version control and wants each developer to start every feature from the same clean state, then throw the environment away afterwards. Separately, the team needs a place to test a release against production-sized data. Which org type fits each need, and why can neither replace the other?
Show answer
Use scratch orgs for the per-feature work. Each one is created from the same definition file and repository, is isolated, and expires on its own. Use a Full sandbox for the release test, because it copies production's metadata and all of its data. A scratch org has no production data or customizations, so it cannot show how the release behaves at production scale. A sandbox cannot be recreated cheaply from a definition file, and it carries production's accumulated configuration, so it does not give a clean starting point.