Sub topic 16
Org shape and scratch org snapshots
This chapter assumes you can already create a scratch org from a definition file and that your Dev Hub is authorized in the CLI as my-hub. It covers two ways to avoid rebuilding the same starting point for every scratch org.
Two ways to start from something other than a blank org
A definition file lists editions, features, and settings. That works well for a clean project. It works poorly when you need your production org's exact configuration, or when setup takes a long time, such as installing packages and loading data.
| Option | What it copies | Source | Typical use |
|---|---|---|---|
| Org shape | Edition, features, limits, and some settings | A real org, such as production or a sandbox | Match a real org's configuration |
| Snapshot | Metadata, installed packages, and data | An existing scratch org | Skip a slow, repeated setup |
An org shape does not copy your metadata or data. It copies only the capabilities of the org. A snapshot copies the contents of a scratch org.
Creating an org shape
Authorize the source org, then create a shape from it. The source org must have shape creation enabled in its Setup under Scratch Org Settings (the exact label can vary by release).
sf org login web --alias my-prod
sf org create shape --target-org my-prod
sf org list shapeThe list shows a shape ID for each source org. To use a shape, add the source org's 15-character ID to the definition file as sourceOrg. Use the real ID of your org, not the placeholder below.
{
"orgName": "Shape-based Scratch Org",
"sourceOrg": "00D000000000000",
"features": [],
"settings": {}
}Then create the scratch org with the same command as before:
sf org create scratch --definition-file config/shape-scratch-def.json --alias shape-scratch --duration-days 7 --target-dev-hub my-hubDelete shapes you no longer need with sf org delete shape --target-org my-prod.
Creating and using a snapshot
Build a scratch org, set it up fully, then snapshot it. The snapshot is stored in the Dev Hub.
sf org create snapshot --source-org my-scratch --snapshot-name BaseSnap --description "Packages and sample data" --target-dev-hub my-hub
sf org get snapshot --snapshot BaseSnap --target-dev-hub my-hubA snapshot is built in the background. Run get until its status is Active. A status of Error means the build failed, and the output includes the reason. Once it is active, new scratch orgs can start from it:
sf org create scratch --snapshot BaseSnap --alias snap-scratch --duration-days 7 --target-dev-hub my-hubUse sf org list snapshot to see what exists.
Common problems
- Shape not found. The shape was created from a different Dev Hub, or shape creation is not enabled in the source org.
- Snapshot stays in progress or fails. Check the status with
get snapshot. Some content in the source org can prevent a snapshot from building. - Stale snapshot. A snapshot does not update when the source scratch org changes. Create a new snapshot after changing the setup.
- Counts against limits. Orgs created from shapes or snapshots still count toward your active and daily scratch org limits.
Exercise
Your team's scratch orgs take 25 minutes to set up because of package installs and data loading. Would you use an org shape or a snapshot, and why?
Show answer
A snapshot. It stores the installed packages, metadata, and data of a configured scratch org, so new orgs skip the slow setup. An org shape copies only edition, features, and limits, so it would not remove the install and load time.