Sub topic 13
Checking active and daily scratch org limits
This chapter shows how to read the two scratch org limits on a Dev Hub, how each limit is counted, and how to check them before you create orgs, especially in scripts. It assumes you have a Dev Hub enabled and authorized in the CLI under the alias my-hub, that sf is installed, and that you can create and delete a scratch org. The jq examples also need jq installed.
Two limits, two different counters
A Dev Hub enforces two separate limits on scratch orgs. Both are checked each time you run sf org create scratch, and creation fails if either one is used up.
| Limit | API name | What it counts | How a slot comes back |
|---|---|---|---|
| Active scratch orgs | `ActiveScratchOrgs` | Scratch orgs that exist now and have not expired or been deleted | Delete the org or let it expire |
| Daily scratch orgs | `DailyScratchOrgs` | Scratch orgs created in the last 24 hours | Wait for the 24-hour window to move past the creation time |
The active limit measures how many orgs exist at this moment. The daily limit measures how many you have created recently. They behave differently when you delete an org:
- Deleting a scratch org lowers the active count right away.
- Deleting a scratch org does not lower the daily count, because the org was still created within the window.
A team that creates and deletes orgs in a tight loop (for example, one per CI run) usually hits the daily limit long before the active one.
The maximum values depend on the edition of the Dev Hub org. Developer Edition and trial Dev Hubs get small allocations. Enterprise, Unlimited, and Performance Edition orgs get larger ones. Salesforce can change these numbers, so read the live values from your own Dev Hub instead of memorizing a table.
Reading the limits with the CLI
The sf limits api display command lists the API limits of an org. Point it at the Dev Hub, not at a scratch org:
sf limits api display --target-org my-hubThe output is a table with one row per limit. Many rows appear, and two matter here. The numbers below are an example only. Your maximums will differ.
Name Remaining Max
ActiveScratchOrgs 38 40
DailyScratchOrgs 77 80Read the columns like this:
Remainingis how many more scratch orgs you can create under that limit right now.Maxis the allocation for the Dev Hub.- Orgs in use is
MaxminusRemaining. In the example, two scratch orgs are active and three were created in the last 24 hours.
To see only the two rows you care about, filter the output:
sf limits api display --target-org my-hub | grep -E "Name|ScratchOrgs"Keep the Name match so the header row stays visible.
If you omit --target-org, the CLI uses your default org. That is often the scratch org, whose limits are unrelated. If ActiveScratchOrgs is missing from the output, you probably queried a non-Dev Hub org.
Finding out which orgs use the slots
Knowing that 38 of 40 slots are free is fine. When the number is low, you also need to know which orgs are holding the slots. The Dev Hub stores one ScratchOrgInfo record per scratch org. Query it with SOQL:
sf data query \
--target-org my-hub \
--query "SELECT SignupUsername, OrgName, Status, CreatedDate, ExpirationDate FROM ScratchOrgInfo WHERE Status = 'Active' ORDER BY CreatedDate DESC"Each row is an org that currently counts toward ActiveScratchOrgs. Look at ExpirationDate to see which orgs will free their slots soon. Look at SignupUsername to find an org you can delete.
Also compare this with your local list:
sf org listsf org list shows only the scratch orgs your CLI has authorized. Orgs created by teammates or by CI on another machine appear in the Dev Hub query but not in your list. This is a common reason for the active count to be higher than you expect.
Deleting orgs to free active slots
To free an active slot, delete an org you no longer need. Use the username from the query, or the alias if the org is in your local list:
sf org delete scratch --target-org my-scratch --no-promptYou can only delete an org you have authorized, or one whose username you can use to authenticate through the Dev Hub. If an org belongs to someone else and you need the slot, ask them first. Deletion cannot be undone.
Remember that deleting does not touch DailyScratchOrgs. If the daily count is the one at zero, deleting does nothing. You must wait.
Checking limits before you create an org
For scripts and CI jobs, check the remaining slots first and stop with a clear message. Failing early is cheaper than waiting for the creation request to be rejected.
The --json flag makes the output machine-readable. The command returns a result array of objects with name, remaining, and max. This script uses jq to read them:
#!/usr/bin/env bash
set -euo pipefail
HUB="my-hub"
limits=$(sf limits api display --target-org "$HUB" --json)
active=$(echo "$limits" | jq '.result[] | select(.name=="ActiveScratchOrgs") | .remaining')
daily=$(echo "$limits" | jq '.result[] | select(.name=="DailyScratchOrgs") | .remaining')
echo "Active slots remaining: $active"
echo "Daily slots remaining: $daily"
if [ "$active" -lt 1 ] || [ "$daily" -lt 1 ]; then
echo "No scratch org capacity left on $HUB. Delete unused orgs or wait." >&2
exit 1
fi
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias my-scratch \
--duration-days 7 \
--set-defaultThis check is not atomic. If several jobs run at once, two of them can both see one remaining slot, and one of them will still fail at creation. For parallel pipelines, treat the pre-check as a courtesy and keep the creation step's own error handling.
Spending limits wisely
Short durations and cleanup keep the active count down. The daily count rewards reuse. These habits help:
- Set
--duration-daysto the time you need. The default is 7 days and the maximum is 30. An org that expires on its own still frees its active slot. - Delete orgs when a feature branch merges, not when they expire.
- Reuse one scratch org for several small changes instead of creating a fresh one for each.
- In CI, delete the scratch org in a final step that runs even when earlier steps fail, so failed runs do not leave orphan orgs.
- Review the
ScratchOrgInfoquery now and then to find orgs left behind by people who have moved on.
If your team keeps reaching the limits, check whether the Dev Hub is in a trial or Developer Edition org. Moving the Dev Hub to a production org with a larger edition raises the allocations. If that is not possible, you can ask Salesforce Support about increasing them.
Recognizing a limit error
When a limit is exhausted, sf org create scratch fails and the message names the limit that was hit. The wording varies between releases, so match on the idea instead of exact text:
- A message about the active scratch org limit means delete an org or wait for one to expire.
- A message about the daily scratch org limit means wait for the rolling window to move on. Deleting will not help.
In both cases, run sf limits api display --target-org my-hub to confirm which counter is at zero before changing anything.
Exercise
Your Dev Hub shows ActiveScratchOrgs Remaining 12 of 40 and DailyScratchOrgs Remaining 0 of 80. You delete five old scratch orgs and try again, but creation still fails. Why, and what are your options?
Show answer
Creation fails because the daily limit is at zero. Deleting orgs only returns active slots. It does not change the daily count, which tracks orgs created in the last 24 hours. Your options are to wait until the oldest creations leave the 24-hour window, to reuse an existing scratch org instead of creating a new one, or to ask for a larger allocation (for example, by using a Dev Hub in a larger edition). Running sf limits api display --target-org my-hub again will show when DailyScratchOrgs Remaining rises above zero.
Exercise
Write the command that lists the usernames and expiration dates of all active scratch orgs on the Dev Hub my-hub, and say why the result might include orgs missing from sf org list.
Show answer
sf data query --target-org my-hub --query "SELECT SignupUsername, ExpirationDate FROM ScratchOrgInfo WHERE Status = 'Active'". The query reads records stored in the Dev Hub, so it includes every active scratch org regardless of who created it. sf org list shows only the orgs authorized on your own machine, so orgs created by teammates or by CI elsewhere are missing from it.