Sub topic 14
Diagnosing common failures
This chapter assumes you have already enabled Dev Hub, authorized it with the sf CLI, and created at least one scratch org. It also assumes you can read a JSON definition file and a terminal error message. Most failures at this stage come from four places: authorization, permissions, the definition file, or limits. You will learn to tell them apart quickly.
Read the error before changing anything
A failed sf org create scratch prints an error name and a message. Treat the message text as a clue, not a fixed string. The exact wording changes between CLI versions and Salesforce releases. Search for the key phrase (such as "limit" or "not a valid") rather than matching whole sentences.
Two habits make diagnosis faster:
- Add
--jsonto a failing command. The output includes an error name, a message, and often suggested actions. - Run
sf --versionandsf org listfirst. Many "mysterious" failures are a wrong CLI version or an alias that points to the wrong org.
The diagram shows a triage order that works for most cases.
Authorization problems
These errors mean the CLI cannot find or use credentials for the org you named.
Alias or username not found. The CLI reports that it has no authorization information for a name. Causes are a typo, an alias created on another machine, or an org that was removed from the local list. Run sf org list --all and compare the names. If the Dev Hub is missing, log in again.
No default Dev Hub. If you omit --target-dev-hub and no default is set, creation fails. Check the setting and set it if needed:
sf config get target-dev-hub
sf config set target-dev-hub=my-hubIf you originally logged in with --set-default-dev-hub, this is already set for that machine. Use sf config list to see whether a value is global or local to the project. A project-level value overrides a global one, so you may be pointing at a different Dev Hub than you expect.
Expired or revoked token. The Dev Hub login can stop working if the session or connected app authorization was revoked, or if the user's password changed. Commands then fail with an invalid session or expired token message. Fix it by logging in again with the same alias:
sf org login web --set-default-dev-hub --alias my-hubThe org is not a Dev Hub. If you authorized a sandbox, or a production org where Dev Hub was never enabled, creation fails with a message saying the org is not a Dev Hub. Sandboxes cannot be a Dev Hub. Confirm that Dev Hub is enabled under Setup, in the Development section, on the Dev Hub page. Enabling Dev Hub cannot be undone, so verify you are in the intended org before turning it on.
Permission problems
Authorization proves who you are. Permissions decide what you may do. A user who logs in to the Dev Hub successfully can still be refused when creating or deleting scratch orgs.
- The user needs the "Create and Delete Scratch Orgs" permission in the Dev Hub. A system administrator typically has it. Other users get it through a permission set.
- The user needs API access, so the profile must allow the API.
- If the login fails before the CLI can finish, check that the user's IP restrictions and login hours allow the connection from your machine.
When an admin colleague can create scratch orgs but you cannot, compare the permission assignments in the Dev Hub. Do not compare your local CLI setups first.
Definition file errors
The scratch org definition file is validated by Salesforce when you submit the request. Errors usually name the bad value. Typical causes:
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a feature is not valid or not available | Misspelled feature name, or a feature that needs a different edition | Check spelling and case, and confirm the edition supports it |
| Error naming a setting | Wrong settings name or a property that does not exist in that API version | Compare against the Metadata API settings reference for your API version |
| JSON parse error | Trailing comma, missing quote, or comment in the file | Validate the JSON in an editor or with a linter |
| Org created but a setting is missing | A setting that depends on a feature that was not listed | Add the required feature to `features` |
| File not found | Wrong relative path | Run the command from the project root, or pass the full path |
Feature and setting names are strict. A common mistake is putting a setting key at the top level instead of inside settings. Another is using a setting name that works in a sandbox but not in a scratch org definition. When you are unsure, reduce the file to the smallest working version and add one item at a time:
{
"orgName": "Minimal Scratch Org",
"edition": "Developer"
}If this creates an org, the problem is in the lines you removed. Adding them back one by one finds the culprit in a few attempts.
Limit errors
Dev Hub enforces two scratch org limits. One caps how many scratch orgs can be active at once. The other caps how many can be created in a rolling 24-hour period. Hitting either one produces an error that mentions a limit, often with an API error code such as LIMIT_EXCEEDED.
Check your remaining capacity against the Dev Hub, not the scratch org:
sf limits api display --target-org my-hubLook for the ActiveScratchOrgs and DailyScratchOrgs rows. Each row shows a remaining value and a maximum. The two limits recover differently:
- Active limit: frees up when you delete a scratch org or one expires. Delete orgs you no longer need with
sf org delete scratch. - Daily limit: recovers as time passes. Deleting an org does not give back a creation from the last 24 hours.
If your team shares one Dev Hub, one person's CI jobs can use the capacity everyone needs. Look in the Dev Hub's Active Scratch Orgs list in the org to see who owns the orgs, and clean up abandoned ones.
Timeouts, stale entries, and leftovers
Creation takes time, and the CLI waits only for a set period. If it stops waiting, the scratch org may still be created on the server. Pass a longer --wait value, or run with --async and finish later with sf org resume scratch. Before creating a duplicate, run sf org list and check the Dev Hub's Active Scratch Orgs list. A duplicate uses up your limit.
Scratch orgs expire silently. After the duration ends, the org is deleted on the server, but your machine still lists it. Commands such as sf org open then fail with an authorization or not-found error. Remove stale entries locally:
sf org list --cleanReview the prompt before confirming, since this removes entries for orgs that are expired or gone.
A final cause of confusing results is the wrong default org. If sf project deploy start lands in an unexpected place, run sf config get target-org and pass --target-org explicitly until the habit is reliable.
A repeatable checklist
- Run
sf --versionandsf org list --all. Confirm the Dev Hub is listed and connected. - Run
sf config get target-dev-hub. Confirm it names the intended Dev Hub. - Re-run the failing command with
--jsonand note the error name. - If the error names a feature or setting, test with a minimal definition file.
- If the error mentions a limit, run
sf limits api display --target-org my-huband delete unneeded orgs. - If the command timed out, check
sf org listbefore retrying.
Exercise
You run sf org create scratch and get a limit error. sf limits api display --target-org my-hub shows ActiveScratchOrgs with 0 remaining and DailyScratchOrgs with 40 remaining. You delete two old scratch orgs. What should you expect when you check the limits again, and what should you do if you then try again and still fail with an authorization error?
Show answer
ActiveScratchOrgs should show 2 remaining, because deleting an org frees an active slot. DailyScratchOrgs should be unchanged, since it counts creations in the last 24 hours. A new authorization error is a separate problem from the limit. Run sf org list --all to check that the Dev Hub is connected, check sf config get target-dev-hub, and log in again with sf org login web --set-default-dev-hub --alias my-hub if the token has expired.
Exercise
A scratch org definition file contains "enableS1DesktopEnabled": true at the top level of the JSON object, and creation fails with an error naming that property. What is the likely mistake and how do you fix it?
Show answer
The setting is placed outside the settings object, and it also needs its parent settings group. Move it to "settings": { "lightningExperienceSettings": { "enableS1DesktopEnabled": true } }, and then retry creation.