Sub topic 1
What scratch orgs are and why teams use them
Prerequisites
This chapter assumes you already have the following:
- Basic familiarity with Salesforce orgs and Setup, including where metadata such as objects, Apex classes, and permission sets lives.
- Comfort running commands in a terminal.
- Basic understanding of JSON, because scratch orgs are configured with a JSON file.
- A Salesforce production, Developer Edition, or trial org where you are a system administrator. You will need it in later chapters to enable Dev Hub.
- The Salesforce CLI (
sf) installed. This chapter does not run any commands, but later chapters do.
You do not need a Dev Hub yet. This chapter explains the concept. Later chapters set it up.
What a scratch org is
A scratch org is a temporary Salesforce org that you create from a text definition and discard when you finish. It is fully functional. You can log in, create records, deploy code, and run tests. It differs from a sandbox or a production org in three ways:
- It is disposable. Every scratch org has an expiration date. The default lifetime is 7 days, and the maximum is 30 days. When it expires, Salesforce deletes it.
- It is defined by configuration. You describe the edition, features, and settings in a file, and Salesforce builds an org that matches. Two people who use the same file get the same kind of org.
- It starts empty. A scratch org has no copy of your production data or metadata. It contains only what the edition provides and what you deploy into it.
Scratch orgs are the main development environment in the Salesforce DX workflow. In that workflow, your project's source files are the source of truth, not any one org. An org is a place to run and test that source.
Why teams use scratch orgs
Teams that develop on shared sandboxes tend to hit the same problems. Changes from different developers overwrite each other. Nobody is sure which sandbox state matches production. Setting up a new environment takes hours of manual clicks. Scratch orgs address these problems directly.
| Problem with shared environments | How scratch orgs help |
|---|---|
| Developers overwrite each other's changes | Each developer or branch gets a private org |
| The environment drifts from what is in version control | The org is built from source, so the source is the reference |
| Manual setup is slow and hard to repeat | A definition file and a deploy command rebuild the org |
| Experiments pollute a long-lived org | A failed experiment is deleted, not cleaned up |
| Tests depend on leftover data or settings | Each test run can start from a known empty state |
Common uses
- Feature development. One scratch org per feature branch. When the branch merges, the org is deleted.
- Continuous integration. A pipeline creates a scratch org, deploys the code, runs Apex tests, reports the result, and deletes the org.
- Testing against a specific edition or feature. You can ask for an edition and features that differ from your production org to see how your code behaves there.
- Package development. Teams that build packages use scratch orgs to develop and validate the package contents in a clean environment.
- Learning and demos. You can try a feature without touching a real org.
How a scratch org is created
A scratch org does not exist on its own. It is created through a separate org called a Dev Hub. The Dev Hub tracks the scratch orgs it created, enforces limits on how many you can have, and records who created each one. The next chapter covers the Dev Hub in detail. For now, keep this model in mind:
- You write a scratch org definition file in your project.
- You run a CLI command that sends the definition to the Dev Hub.
- The Dev Hub provisions a new, separate org and records it.
- You deploy your source to the new org and work in it.
- When the org expires or you delete it, the Dev Hub frees its slot.
What a definition file controls
You will write a definition file in a later chapter. This preview shows what it can control, so you know what "defined by configuration" means in practice:
{
"orgName": "My Project Scratch Org",
"edition": "Developer",
"features": ["EnableSetPasswordInApi"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}The file sets four things:
orgNameis the company name shown in the org.editionselects the base edition, such as Developer or Enterprise.featuresturns on optional capabilities that are not part of the edition by default.settingsenables org settings that you would otherwise change by hand in Setup.
Because this file lives in version control with your code, a change to the org's setup is reviewed and tracked like any other change.
Source tracking
Scratch orgs support source tracking. The CLI and the org keep a record of which metadata components changed in the org and which changed locally. This lets the CLI deploy only your local changes and retrieve only the changes made in the org. Sandboxes support source tracking only for some types, and production orgs do not support it. For scratch orgs it is the default behavior, and it is one reason they suit an iterative workflow.
Limits and trade-offs
Scratch orgs are not a replacement for every environment. Keep these points in mind:
- They are temporary. Anything you build by hand and do not capture in source is lost on expiry. Retrieve changes into your project before the org goes away.
- They have no production data. If a test needs realistic data, you must load sample data yourself.
- They are limited in number. A Dev Hub allows a fixed number of active scratch orgs and a fixed number of creations per day. The limits depend on the Dev Hub's edition. A busy CI pipeline can use up the daily allowance if it never deletes finished orgs.
- Creation takes time. Creating a scratch org usually takes from several seconds to a few minutes. Plan pipelines accordingly.
- They are not a staging environment. Final validation against production-like data and integrations belongs in a sandbox or a similar pre-production org.
Scratch orgs compared with other environments
| Property | Scratch org | Developer sandbox | Developer Edition org |
|---|---|---|---|
| Lifetime | 1 to 30 days | Until refreshed or deleted | Persistent |
| Created from | A definition file | A copy of production metadata | Sign-up form |
| Contains production data | No | No (metadata only) | No |
| Source tracking | Yes | Supported for some metadata | No |
| Best for | Per-feature and CI work | Shared integration and testing | Personal learning |
Exercises
Exercise
A teammate says, "I built a custom report type in my scratch org through Setup, and the org expired last night. Can you restore it for me?" What is the answer, and what should the teammate have done?
Show answer
The org cannot be restored, because Salesforce deletes expired scratch orgs permanently. The teammate should have retrieved the report type into the project source (with sf project retrieve start) and committed it to version control before the org expired. With the source saved, any new scratch org can receive it with a deploy.
Exercise
Your CI pipeline creates a scratch org for every pull request and runs tests, but it never deletes the orgs. After a busy day, new runs start failing at the creation step. Which property of scratch orgs explains this, and how would you change the pipeline?
Show answer
The Dev Hub limits both the number of active scratch orgs and the number created per day. Orgs that are never deleted keep occupying active slots, and each creation also counts toward the daily limit. Add a final pipeline step that deletes the scratch org after the tests finish, whether they pass or fail, and consider a short duration so any missed org expires quickly.