Sub topic 3
Creating and refreshing sandboxes
This chapter covers how a sandbox is created, how it ages, and how a refresh resets it. It assumes you know the difference between production and non-production orgs, can deploy metadata between orgs, and are comfortable at the command line. The previous chapter compared the four sandbox types. Here the focus is on the operations: creating, cloning, refreshing, and deleting.
What a sandbox copy is
A sandbox is a copy of a source org. For most sandboxes the source is production. The copy always includes metadata (configuration and code). Whether it includes data depends on the type:
- A Developer or Developer Pro sandbox copies metadata only.
- A Partial Copy sandbox copies metadata plus a sample of data defined by a sandbox template.
- A Full sandbox copies metadata plus all data. You can also choose whether to include field tracking history and Chatter data.
The copy is a snapshot taken at one moment. After that, production and the sandbox drift apart. A refresh is how you bring the sandbox back in line.
Limits that govern creation and refresh
Your production org's edition and licenses set how many sandboxes of each type you can have. You cannot create a sandbox of a type when you have used all licenses of that type.
Each type also has a refresh interval, the minimum wait before you can refresh the same sandbox again. The interval is counted from when the sandbox was created or last refreshed.
| Type | Data copied | Data storage limit | Refresh interval |
|---|---|---|---|
| Developer | Metadata only | 200 MB | 1 day |
| Developer Pro | Metadata only | 1 GB | 1 day |
| Partial Copy | Metadata and sampled data | 5 GB | 5 days |
| Full | Metadata and all data | Same as production | 29 days |
Check your own org's Sandboxes page in Setup for your actual license counts, since allocations vary by edition and purchased add-ons.
Sandbox names and usernames
Every sandbox has a name. The name must be alphanumeric and at most 10 characters long. Salesforce appends the name to each copied username. If a production user is [email protected] and the sandbox is dev1, that user logs in to the sandbox as [email protected].
Pick names that describe a purpose or an owner, such as qa, uat, or dev1. A name is tied to the sandbox record. When you refresh, the name stays the same, so scripts that depend on the username keep working.
Creating a sandbox with the CLI
You can create a sandbox in Setup, or with the Salesforce CLI. For the CLI, first write a definition file. This one requests a Developer sandbox named dev1.
{
"sandboxName": "dev1",
"licenseType": "Developer"
}Valid licenseType values are Developer, Developer_Pro, Partial, and Full. Partial Copy sandboxes also need a sandbox template, which you reference in the definition file with templateId. A definition file can also name an Apex class to run after the copy through apexClassId. The later chapter on post-copy automation covers that.
Create the sandbox from an authorized production org. The alias prod below is the alias you gave that org at login.
sf org login web --alias prod
sf org create sandbox --definition-file config/dev-sandbox-def.json --alias dev1 --target-org prodThe command waits while Salesforce builds the copy. If your terminal times out or you interrupt it, the sandbox keeps building on the server. Resume tracking it with:
sf org resume sandbox --name dev1Use sf org list to see the sandbox in your list of authorized orgs once it is ready.
Creation time varies a lot. A Developer sandbox can be ready in minutes. A Full sandbox of a large production org can take hours, or longer. Plan around that when you schedule a test cycle.
The sandbox lifecycle
A sandbox moves through a small set of states. Knowing them tells you what you can and cannot do at each point.
While a sandbox is pending or processing, you cannot log in to it. After it is active, you can log in, deploy, and test. You can refresh only after the refresh interval has passed.
Cloning from another sandbox
The source of a copy does not have to be production. You can create a sandbox from an existing sandbox. This is called a clone. A clone takes the source sandbox's metadata and, if the source sandbox holds data, that data too, subject to the new sandbox's type.
Cloning is useful when a team has built up configuration in one sandbox that does not yet exist in production. For example, an integration team may have a configured UAT sandbox, and you want an identical one for a second project. In the CLI, pass the source sandbox name with --clone instead of using production as the source:
sf org create sandbox --clone uat --name uat2 --alias uat2 --target-org prodTreat a clone as a shortcut and not a standard. A clone of a clone copies any untracked changes and drift along with it.
Refreshing a sandbox
A refresh replaces the sandbox contents with a new copy of its source. The sandbox keeps its name, license type, and place in your allocation.
What a refresh does to your work:
- Everything in the sandbox that is not in the source org is lost. This includes metadata you changed only in the sandbox, test records, and debug logs.
- The sandbox gets production's current metadata and, for Partial and Full types, current data.
- Users are copied again with refreshed usernames, and the same email protections apply as for a new copy.
Refresh from Setup by opening Sandboxes, finding the sandbox row, and choosing Refresh. You can change the license type at this step, for example from Developer to Developer Pro, if you have a free license of the new type. A refresh then follows the same lifecycle as creation, starting at Pending.
After a refresh, your local CLI authorization for that sandbox may stop working. If a command reports an authentication error, log in again with sf org login web and the sandbox's instance URL, or log in with --instance-url https://test.salesforce.com.
Why refresh matters
Sandboxes go stale. Production receives hotfixes, new fields, and new data. A sandbox that was last refreshed months ago no longer reflects production, so tests there can pass while the same change fails in production.
A common schedule is to refresh by purpose:
- Developer sandboxes, which are cheap, are refreshed often, even before each feature.
- A QA or UAT sandbox is refreshed at the start of a release cycle.
- A Full sandbox used for performance or staging tests is refreshed on a calendar, since the interval is 29 days at best.
The refresh interval also limits how fast you can recover from a mistake. If you refresh a Full sandbox by accident, you cannot refresh it again for up to 29 days.
Deleting a sandbox
Delete a sandbox when you no longer need it. This frees the license for a new sandbox of that type. Deleting is permanent, so confirm that nothing in it is unsaved.
sf org delete sandbox --target-org dev1 --no-promptThe --no-prompt flag skips the confirmation question. Use it in scripts, and leave it off when you run the command by hand.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Create fails with a license error | All licenses of that type are in use | Delete an unused sandbox of the same type, or choose another type |
| Refresh option is unavailable | The refresh interval has not passed | Wait until the interval ends, or create a new sandbox if a license is free |
| Name rejected | The name has non-alphanumeric characters or is longer than 10 characters | Use a shorter alphanumeric name |
| Partial Copy creation fails | No sandbox template was supplied | Create a template in production and set `templateId` |
| CLI cannot reach the sandbox after a refresh | Stored authorization is out of date | Log in again |
Exercise
A Developer Pro sandbox named qa1 was refreshed on Monday. On Wednesday, a developer discovers that production now has a new validation rule that the sandbox lacks, and asks you to refresh qa1 so tests match production. Can you refresh it, and what must you do before you do?
Show answer
Yes. The Developer Pro refresh interval is 1 day, and two days have passed, so the refresh option is available. Before refreshing, retrieve or commit any metadata and save any test data that exist only in qa1, because a refresh replaces the sandbox contents with a new copy of production and discards everything else.
Exercise
You start sf org create sandbox for a Full sandbox and your terminal session closes after 20 minutes. Is the sandbox lost, and how do you continue?
Show answer
No. Salesforce keeps building the sandbox on the server regardless of your terminal. Run sf org resume sandbox --name <sandbox name> to resume tracking the creation, and then use sf org list to confirm the sandbox is authorized when it finishes.