Sub topic 11
What a refresh does and when to use it
This chapter assumes you know the four sandbox types and their limits from the earlier chapters. It covers what a refresh changes, what it leaves alone, and how to decide whether you need one.
What a refresh does
A refresh rebuilds an existing sandbox from its source org. The source is production or, for a cloned sandbox, another sandbox. Salesforce takes a new copy of the source and replaces the sandbox's contents with it. The sandbox keeps its name, and you do not create a new one.
A refresh changes two things:
- Metadata. Configuration and code (objects, fields, flows, Apex classes, page layouts, permission sets) are replaced with the source org's current versions.
- Data. Records are replaced according to the sandbox type. A Developer or Developer Pro sandbox gets no records. A Partial Copy sandbox gets a sample selected by a sandbox template. A Full sandbox gets a copy of the source data.
Anything you built in the sandbox that is not in the source org is lost. This includes a half-finished Apex class, a new custom field, test records, and changed settings. A refresh is a reset to the state of the source.
The refresh lifecycle
The process has a predictable order:
- You start the refresh in Setup. You may choose a sandbox template if the type supports one.
- Salesforce copies the source org into a new sandbox copy. The existing sandbox remains available while this happens.
- When the copy is done, Salesforce emails you.
- You activate the new copy. Activation replaces the old sandbox, and the old contents are gone for good.
- Post-copy work starts, such as running a
SandboxPostCopyclass and fixing integration settings.
The later chapters cover steps 1, 4, and 5 in detail. For now, remember two points. The old sandbox stays usable until you activate, so you can keep working while the copy builds. Anything you change in the old sandbox after the copy starts is not in the new copy.
The copy reflects the source org at the moment the copy runs. Changes made in production after that point do not appear until the next refresh.
What a refresh does not do
A refresh does not move your work back to production, and it does not merge anything. It only pulls changes from the source to the sandbox. Changes flow back through deployments (change sets, the Metadata API, or the Salesforce CLI), never through a refresh.
A refresh also does not reuse the old sandbox's settings. Because the new copy comes from the source org, the sandbox gets the source org's configuration. Some values that were specific to the sandbox, such as endpoints for integrations or custom settings that you tuned for testing, go back to what the source holds. You will need to reapply them, ideally with automation.
Refresh intervals and limits
Each sandbox type has a minimum interval between refreshes. The interval is counted from when the sandbox was last created or refreshed.
| Sandbox type | Data copied | Refresh interval |
|---|---|---|
| Developer | Metadata only | 1 day |
| Developer Pro | Metadata only | 1 day |
| Partial Copy | Metadata and a sample of data | 5 days |
| Full | Metadata and all data | 29 days |
Check your edition's limits in Setup before you plan. The interval means a refresh of a Full sandbox is a scarce action. If you refresh it today for a bad reason, you cannot refresh it again for about a month.
When to refresh
Refresh when the sandbox no longer reflects what you need to test against. Common triggers:
- Metadata drift. Production has had many changes that never went to the sandbox, so tests pass in the sandbox but fail in production.
- Stale or dirty data. QA testing needs data that looks like current production, or test records have piled up and obscure results.
- Before a major project. You are starting a new development cycle and want a clean baseline that matches production.
- After a production release. A new set of changes is live and the sandbox should include it.
- A broken sandbox. Corrupt configuration, failed deployments, or unclear state make the sandbox unreliable, and starting over is faster than repairing it.
- Data masking or compliance. Production data has changed what you are allowed to have in lower environments, and you need to replace what the sandbox holds.
When not to refresh
A refresh is the wrong tool in these cases:
- You just need one change from production. Retrieve or deploy that component instead of resetting the whole sandbox.
- The sandbox holds unreleased work. If the work has not been committed to version control or deployed, refreshing will erase it. Save it first.
- You want to avoid using up the interval. For a Full sandbox, one refresh may consume the next month's allowance.
- Several teams share the sandbox. A refresh affects everyone who uses it. Coordinate first, or give each team its own sandbox.
Use this quick comparison to decide:
| Situation | Better choice |
|---|---|
| One field or class is missing in the sandbox | Deploy that component |
| The whole sandbox is badly out of date | Refresh |
| You need a second environment for a new project | Create or clone another sandbox |
| Test data is messy but metadata is current | Delete test records, or refresh a Partial Copy sandbox |
Check which org you are in
Because a refresh is destructive, make sure you are in the sandbox and not production when you run cleanup code. This query reads the IsSandbox field of the Organization object:
Organization org = [SELECT Id, IsSandbox FROM Organization LIMIT 1];
if (org.IsSandbox) {
System.debug('Running in a sandbox');
} else {
System.debug('Running in production');
}Run it in Anonymous Apex after activation to confirm that you are in the refreshed sandbox. Post-refresh code should do the same check before it changes anything.
Refresh versus creating a new sandbox
Both actions produce a sandbox from a source, but they differ in what is left behind. Refreshing keeps the sandbox's name and its slot in your org's allowance. Creating a new sandbox uses up an additional slot and leaves the old one untouched. If you need to preserve the old sandbox's state, for example to compare behavior or to keep a test environment alive, create a new sandbox rather than refreshing. If a slot is limited, refresh instead.
Common mistakes
- Activating without checking. The new copy replaces the old one right away. Verify that nobody needs the old state first.
- Assuming settings carry over. Integration endpoints, named credentials, and user emails come from the source copy and need review afterward.
- Refreshing to fix a deployment. If a deployment failed, fix the cause. A refresh only hides problems that will return.
- Spending a Full sandbox refresh on a small issue. The 29-day interval makes this costly.
Exercise
Your team has a Full sandbox that was refreshed 10 days ago. A developer reports that one custom field added in production last week is missing in the sandbox, and a tester has half-finished work in the sandbox that is not saved elsewhere. Should you refresh? Explain, and name the better action.
Show answer
No. A Full sandbox cannot be refreshed again until 29 days have passed since the last refresh or creation, so the refresh is not available yet. It would also be the wrong tool, because it would erase the tester's unsaved work and replace the whole sandbox to fix one field. The better action is to deploy or retrieve just the missing field into the sandbox, and have the tester save their work to version control.