Sub topic 12
Preparing for a refresh: protect your work
This chapter assumes you know what a sandbox is, how the four sandbox types differ, and what a refresh does to a sandbox. You should also be comfortable with Setup and have the Salesforce CLI (sf) installed. The next chapter covers the refresh steps themselves. This one covers what you do before you start one.
Why preparation matters
A refresh rebuilds the sandbox from production. Metadata (configuration and code) in the new copy matches production as of the moment the copy starts. Data depends on the sandbox type: a Full sandbox copies production records, a Partial Copy sandbox copies a sample, and a Developer sandbox copies none. Anything that exists only in the old sandbox is gone once the new copy is activated. That includes:
- Apex classes, flows, and fields that were never deployed to production.
- Test records you created by hand.
- Settings you changed only in the sandbox, such as custom setting values, named credentials, and email settings.
- Outbound change sets and deployment history stored in the sandbox.
Salesforce does not recover these items after activation. Protecting them is your job.
The old sandbox stays available until you activate the new copy. That gives you a window to rescue work, but don't depend on it. Activation can be scheduled or automatic, and other people may keep working in the old sandbox without realizing it is about to be replaced.
The preparation workflow
1. Check the interval and the sandbox type
Each sandbox type has a minimum interval between refreshes: 1 day for Developer and Developer Pro, 5 days for Partial Copy, and 29 days for Full. If you start a refresh and then find that you needed something from the old copy, you cannot refresh again right away. A Full sandbox mistake can cost you almost a month, so be most careful there.
Also confirm that the sandbox is the right type for the data you need after the refresh. If you need production records for testing and the sandbox is a Developer sandbox, a refresh will not bring them back. A sandbox template can limit which records a Partial Copy or Full sandbox receives. Review the template before you refresh.
2. Tell everyone who uses the sandbox
Send a notice with the refresh date and the deadline for saving work. Ask each person to confirm that they have deployed or backed up their changes. For a shared integration or QA sandbox, name one person to approve the start of the refresh. Without that approval, someone may find out after activation that a week of test setup is gone.
3. Deploy finished work to production
Work that is complete should already be in production, or on its way there, before the refresh. Production is the source of the new copy, so anything deployed there is carried into the new sandbox.
Check three things:
- Every change set or deployment that is ready has been validated and deployed.
- Nothing is half deployed. A partial deployment leaves the new sandbox with a state that does not match either old or new.
- Production has no changes you do not want copied into the sandbox, such as an unfinished release.
4. Retrieve unfinished metadata
Work that is not ready for production still needs a safe place to live. Retrieve it into a source control repository, not into a laptop folder. If your team does not already keep the sandbox in source control, this is a good moment to start.
First generate a manifest from the sandbox. Then retrieve the metadata it lists.
sf project generate manifest --from-org dev1 --output-dir manifest
sf project retrieve start --manifest manifest/package.xml --target-org dev1
git add force-app manifest
git commit -m "Save dev1 metadata before refresh"Manifest generation from an org covers many metadata types but not all of them. Check that the pieces you care about are in the retrieved folder. If a component is missing, add it to package.xml by hand or retrieve it with the Metadata API directly.
After a refresh, you can deploy the saved work back to the sandbox with sf project deploy start --target-org dev1. Deploy only the pieces that still make sense. The refreshed sandbox may already contain newer production versions of some of the same components. Review conflicts before you overwrite them.
5. Export data you cannot recreate
A Developer sandbox comes back with no records, and Partial Copy and Full sandboxes come back with production data, not your test data. If you built test records by hand, export them before the refresh.
For a small, related set of records, the CLI can export a data tree that you can import later:
sf data export tree \
--query "SELECT Id, Name, Industry, (SELECT LastName, Email FROM Contacts) FROM Account WHERE Name LIKE 'QA %'" \
--plan \
--output-dir data \
--target-org dev1For larger volumes, use Data Loader or the Bulk API to write CSV files. Keep these points in mind:
- Record IDs change after a refresh. Do not rely on stored IDs. Use external ID fields to rebuild relationships on import.
- Exports can contain personal data. Keep them off shared drives and delete them when you no longer need them.
- Files, attachments, and rich text images may need separate export steps.
6. Record sandbox-only settings
Some settings live only in the sandbox and are easy to forget. Write them down before you refresh. The list below shows what to capture:
| Setting | Why it matters |
|---|---|
| Named credentials and external credentials | Integration endpoints and secrets in a sandbox usually point to test systems. |
| Custom setting and custom metadata values | Test values may differ from production values. |
| Remote site settings and CORS entries | Needed for callouts to test services. |
| Email deliverability setting | Resets to a restricted level after a refresh. |
| Scheduled jobs and flows | Scheduled Apex jobs may need to be started again. |
| Test user records, permission set assignments, and passwords | Usernames change, and users must be set up again. |
| Connected app and OAuth settings | Callback URLs often differ between environments. |
Never write real secrets into a shared document. Store them in a password manager or secrets vault, and record only where each one is kept.
If you set up the same items after every refresh, the next chapters show how to automate that work. A SandboxPostCopy Apex class is the standard way to do it.
Common mistakes
- Starting the refresh from the wrong sandbox. Names such as
dev1anddev2look alike. Check the sandbox name on the confirmation screen. - Trusting that deployment history is saved. Change set records in the sandbox are replaced. Keep your own release notes.
- Forgetting that a refresh is a replacement, not a merge. Salesforce does not combine old and new. Work you did not save is lost.
- Refreshing during a release. Production metadata is copied as it stands at that moment. A half-finished release copies half-finished.
- Skipping a check on storage. If production data exceeds the sandbox storage limit, the copy can fail or be restricted. Check the sizes and the template before you start.
A quick readiness check
Before you start, confirm each statement is true:
- All finished work is deployed to production.
- All unfinished metadata is committed to source control.
- Required test data is exported and can be reloaded.
- Sandbox-only settings are documented, with secrets stored safely.
- Users have been told the date and have confirmed that their work is saved.
- The refresh interval, sandbox type, and template have been reviewed.
If any item is false, wait. A short delay costs far less than lost work.
Exercise
Your team's Developer Pro sandbox contains a half-finished Apex class and 200 hand-built test accounts. A refresh is scheduled for tomorrow. List what you do to protect the work, and say why record IDs matter during re-import.
Show answer
Retrieve the Apex class (and related metadata) into source control with a generated manifest and sf project retrieve start, since it is not ready for production. Export the test accounts with sf data export tree or Data Loader, because a Developer Pro sandbox returns with no data. Record sandbox-only settings, such as named credentials and custom setting values, and tell users the deadline. Record IDs matter because the refreshed sandbox assigns new IDs, so relationships must be rebuilt on import with external ID fields, not with the old IDs.
Exercise
Why is preparation especially important before refreshing a Full sandbox, compared with a Developer sandbox?
Show answer
A Full sandbox can be refreshed only once every 29 days, while a Developer sandbox can be refreshed once a day. If you refresh a Full sandbox and discover you lost something, you cannot refresh again to retry for almost a month, so missing work or settings stay a problem for much longer.