Sub topic 7
Scratch org lifespan and limits
This chapter assumes you can create a scratch org from a Dev Hub with the Salesforce CLI and write a definition file. It covers how long a scratch org lives and how many a Dev Hub lets you create.
Scratch orgs are temporary
A scratch org has a fixed lifespan, set when you create it. The duration is 1 to 30 days, and the default is 7. When the date passes, Salesforce deletes the org and everything in it. You cannot extend the duration after creation.
This follows from the purpose of scratch orgs. Your source in version control is the source of truth, and the org is a disposable build of it. Anything you configured by hand and did not retrieve into the project is lost on expiry.
You set the duration with --duration-days:
sf org create scratch --definition-file config/project-scratch-def.json --alias feature-123 --duration-days 3 --set-default
sf org listsf org list shows each scratch org with its expiration date, so you can see which ones are about to disappear.
Choose the shortest duration that fits the work:
| Use | Typical duration |
|---|---|
| Automated test run in CI | 1 day |
| Single feature or bug fix | 3 to 7 days |
| Longer feature, demo, or review | 14 to 30 days |
Short lifespans keep the Dev Hub tidy and limit configuration drift, which is the gradual difference between an org and its source.
Two limits on creation
Your Dev Hub enforces two separate limits:
- Active scratch orgs. The number of scratch orgs that exist at one moment and have not expired or been deleted.
- Daily scratch orgs. The number you may create in a rolling 24-hour period.
Both depend on the Dev Hub's edition. Trial Dev Hubs get very small allocations, and paid editions get larger ones. The numbers change over time, so read them from your own org instead of relying on a table.
sf org list limits --target-org devhubThe output lists ActiveScratchOrgs and DailyScratchOrgs, each with a Max and a Remaining value. Look at Remaining before you start a large batch of work.
How the limits behave
The two limits count different things, so freeing one does not always help:
- Deleting a scratch org frees an active slot immediately. It does not give back the daily creation you already used.
- The daily limit is a rolling window. If you exhaust it, you wait for earlier creations to age out of the 24 hours.
- Expired orgs stop counting as active, but until they expire, forgotten orgs keep using slots.
A common failure is a CI pipeline that creates an org per commit and never deletes it. The active limit fills, and then every build, and every developer's manual creation, fails. Delete the org at the end of each run, even when the tests fail.
sf org delete scratch --target-org feature-123 --no-promptPractical habits
- Name scratch orgs with an alias that identifies the work, such as the ticket number, so you know what is safe to delete.
- Run
sf org list limitsas a pre-check in scripts and stop with a clear message ifRemainingis zero. - Push or retrieve your changes before the expiration date. Check the date with
sf org list. - Use short durations by default and raise them only when needed.
Exercise
A CI job creates a scratch org for every pull request and deletes it only when tests pass. Builds start failing at org creation even though few developers are working. What is the likely cause, and what two changes would fix it?
Show answer
Orgs from failed runs are never deleted, so they keep counting against the active scratch org limit until they expire. Fix it by deleting the org in a step that always runs, whether tests pass or fail, and by creating CI orgs with a short duration such as 1 day so any leaked org expires soon. Checking sf org list limits before creation also gives a clear error message.