Sub topic 12
Managing scratch org lifetime and cleaning up
A scratch org is temporary by design. This chapter covers how long one lives, how to see when it will expire, how to delete it early, and how to clear the stale entries it leaves on your machine. It assumes you have already authorized a Dev Hub and created a scratch org with sf org create scratch, as covered in earlier chapters.
How long a scratch org lives
You set the lifetime when you create the org, using --duration-days. The value is a whole number of days from 1 to 30. If you leave the flag out, the org lasts 7 days.
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --duration-days 3 --set-defaultPick the shortest duration that fits the work:
- A one-day org suits a quick experiment or a single automated test run.
- A 7-day org suits a feature branch with a normal review cycle.
- A 30-day org suits long-running work, but it holds one of your active slots for the whole period.
Two rules shape how you plan around this:
- The expiration date is fixed at creation. You cannot extend a scratch org later.
- When the org expires, Salesforce deletes it and its data. Anything that exists only in the org is lost.
If you need more time, create a new org from the same definition file and deploy your source to it. This is why you keep source in version control and treat the org as disposable.
The lifecycle of a scratch org
A scratch org moves through a small set of states. The Dev Hub tracks the org, while your local machine keeps a separate record of the authorization.
The last step is easy to miss. The CLI stores the org's alias and login information on your computer. When the org expires, that local entry stays behind until you remove it.
Finding out when an org expires
sf org display shows details for one org, including its expiration date for a scratch org.
sf org display --target-org my-scratchLook for the Expiration Date row in the output. For scripts, add --json and read the same value from the result.
sf org list shows every org the CLI knows about. Scratch orgs appear in their own section with their alias, username, and expiration date.
sf org listExpired orgs can still appear in this list until you clean them up. The list checks the connection status of each org, so a dead org shows up as expired or as not connected rather than disappearing. If the command is slow because you have many orgs, add --skip-connection-status to skip that check.
Deleting a scratch org early
You do not have to wait for expiry. Delete an org as soon as you finish with it, because each live org takes one of your Dev Hub's active scratch org slots.
sf org delete scratch --target-org my-scratchWithout extra flags, the CLI asks you to confirm. In scripts and CI jobs, skip the prompt:
sf org delete scratch --target-org my-scratch --no-promptDeletion is permanent. Before you delete, make sure nothing valuable exists only in the org. If you changed metadata in the org with point-and-click tools, pull it into your project first:
sf project retrieve start --target-org my-scratchThen commit the retrieved files. After the delete, the org cannot be recovered.
What deletion does and does not change
- The org stops working immediately. Its login URL no longer opens.
- The Dev Hub frees one active scratch org slot.
- The daily creation count does not go back down. The daily limit counts how many orgs you created in a rolling 24-hour window, not how many exist now. Creating and deleting in a loop can therefore hit the daily limit even when few orgs are active.
- The Dev Hub keeps a record of the org with a status showing it is no longer active. You can review past orgs from the Dev Hub's scratch org records in the Salesforce UI.
Cleaning up local entries
After an org expires or is deleted, your CLI still holds an entry for it. Remove those entries with:
sf org list --cleanThe command lists orgs, finds the ones that no longer exist, and asks whether to remove their local authorization files. Add --no-prompt to remove them without asking.
The clean step does not delete anything in Salesforce. It only edits files on your machine. It also does not touch your Dev Hub authorization or any other live org.
A related problem is a stale default. If my-scratch was your default target org and it is gone, later commands that rely on the default fail with a message about a missing or invalid org. Fix it by creating a new org with --set-default, or by choosing a different default:
sf config set target-org my-other-scratchA cleanup routine for a team or CI
On a busy project, orgs pile up. A simple routine keeps the Dev Hub healthy:
- Name aliases after the work, such as
feature-loginorpr-142, so you know which to delete. - Delete the org when the branch merges or the pull request closes.
- Run
sf org list --cleanregularly on your own machine. - Check Dev Hub limits when creation starts to fail (covered in the next chapter).
This script deletes a list of aliases and then cleans up. It ignores failures for orgs that are already gone, so it is safe to run twice.
#!/usr/bin/env bash
# Usage: ./cleanup.sh feature-login pr-142
for alias in "$@"; do
echo "Deleting $alias"
sf org delete scratch --target-org "$alias" --no-prompt || echo "Skipped $alias"
done
sf org list --clean --no-promptCI systems start from a clean machine, so local entries matter less there. The deletion still matters, because an org left behind keeps using an active slot until it expires. In CI, delete the org in a step that always runs, even when the tests fail.
Common problems
| Symptom | Likely cause | What to do |
|---|---|---|
| An org in `sf org list` shows as expired | The duration ended | Run `sf org list --clean`, then create a new org |
| `sf org delete scratch` says no org was found | The org already expired or was deleted | Run `sf org list --clean` to remove the local entry |
| Commands fail after an org expires | The deleted org was your default target | Set a new default with `sf config set target-org` |
| Creation fails though few orgs are active | You hit the daily limit | Wait for the 24-hour window to pass, or check limits |
| Work disappeared after expiry | Changes were never retrieved or committed | Keep source in version control and retrieve often |
Key points
- Duration runs from 1 to 30 days and defaults to 7. You set it at creation and cannot extend it.
sf org displayandsf org listshow the expiration date.sf org delete scratchfrees an active slot immediately but does not reduce the daily creation count.sf org list --cleanremoves stale local entries and changes nothing in Salesforce.- Retrieve and commit source before you delete.
Exercise
You created a scratch org with --duration-days 5 on Monday. On Wednesday you realize you need it through the following Friday. Can you extend it? Also, you delete it on Thursday and then run sf org list. Why might the deleted org still appear, and which command removes it from the list?
Show answer
No. A scratch org's expiration is fixed at creation, so you cannot extend it. You must create a new org from the same definition file and deploy your source to it. Before the old org goes away, retrieve and commit any changes made in the org. The deleted org can still appear in sf org list because the CLI keeps a local authorization entry after the org is gone. Run sf org list --clean (add --no-prompt to skip the confirmation) to remove the stale entry. This changes only your local files.
Exercise
A teammate runs a script that creates a scratch org and deletes it, 12 times in one hour. The 12th creation fails even though no scratch orgs are active. Which limit did they most likely reach, and why did deleting the orgs not help?
Show answer
They most likely reached the daily scratch org limit. This limit counts how many orgs were created in a rolling 24-hour window, so deleting an org frees an active slot but does not give back the daily allocation. Check the DailyScratchOrgs entry with sf limits api display --target-org my-hub, and wait for the window to pass or reduce how often the script creates orgs.