DEV Community

Rohan Mehta
Rohan Mehta

Posted on

Salesforce Scratch Orgs: Setting Up a Disposable Dev Environment That Actually Works

Salesforce Scratch Orgs: Setting Up a Disposable Dev Environment That Actually Works

Sandboxes are great for testing in a copy of production. But when you're building something new — a trigger, a Lightning Web Component, a flow — a full sandbox is slow to refresh, shared with other people, and full of data you don't need. That's what scratch orgs are for: a fresh, empty Salesforce org you can spin up in minutes, break freely, and throw away when you're done.

What a scratch org actually is

A scratch org is a source-driven, temporary Salesforce org. Instead of copying production, you define it in a JSON file: which edition, which features, how long it lives. You push your metadata in from your local project, and the org exists only as long as you need it — 1 to 30 days, then it expires on its own.

The key mindset shift: the org is disposable. Your source of truth is your local project, not the org. If something goes wrong, you delete the scratch org and make a new one. That freedom changes how you develop — you experiment more because the cost of a mistake is near zero.

Setting one up

First, enable Dev Hub in your production or Developer Edition org. That's a one-time step in Setup, and it gives you the ability to create scratch orgs.

Next, your scratch org definition file. A minimal one looks like this:

{
  "orgName": "Way2Force Dev",
  "edition": "Developer",
  "features": ["EnableSetPasswordInScratchOrg"],
  "settings": {
    "lightningExperienceSettings": {
      "enableS1DesktopEnabled": true
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Then create it from the CLI:

sf org create scratch --definition-file config/project-scratch-def.json --alias my-feature --duration-days 7 --set-default
Enter fullscreen mode Exit fullscreen mode

A few minutes later you have a working org. Open it with sf org open, push your code with sf project deploy start, and you're building.

A workflow that works

One scratch org per feature. Don't reuse a single scratch org for everything — when you finish a feature, delete it and start fresh for the next one. It keeps state from leaking between tasks.

Push early, push often. Deploy after every meaningful change. Because the org is empty, a deploy takes seconds, and you catch errors while the change is still fresh in your head.

Use a data script. Scratch orgs start empty, which is a feature — but you need test data. Keep a small script or a set of data import files in your project so every new org gets the same accounts, contacts, and custom records in one command.

Set a short duration. Seven days is a good default. A scratch org that lives too long starts accumulating the same cruft you're trying to avoid. If you need more time, you can always create another one.

Gotchas worth knowing

  • Scratch orgs expire. Anything not pushed back to your project is gone when the org dies. Commit your changes before the expiry date.
  • No production data. If your feature depends on realistic data volumes, a partial-copy sandbox is the better tool.
  • Feature parity isn't total. Some features need specific settings in your definition file, and a few things simply aren't available in scratch orgs. Check the docs before assuming a feature works.
  • Limits exist. You get a fixed number of active scratch orgs per day and per Dev Hub. Delete finished ones promptly — don't let them sit.

Scratch orgs vs sandboxes: when to use which

Use a scratch org when you're building something new in isolation — a trigger, a component, a flow — and you want a clean room. Use a sandbox when you need production-like data, when you're testing a deployment against real configuration, or when several people need to work in the same environment.

Most teams end up using both: scratch orgs for daily development, sandboxes for integration testing and UAT. That's the right split.

The testing habit that makes it pay off

A scratch org is only useful if you actually test in it. Before you push a change anywhere else, run your test classes in the scratch org. It takes minutes and catches the kind of bug that costs hours in a shared sandbox.

If you're not sure what good coverage looks like, our guide to Salesforce test classes and code coverage walks through the rules, the gotchas, and how to write tests that actually help. And if your development setup still involves a lot of manual clicking, our Claude Code setup guide for Salesforce shows how an AI coding assistant fits into this workflow.

Start with one scratch org for your next feature. Once you've worked in an environment you can break without consequences, going back to a shared sandbox for development feels like writing code directly in production.


Originally published at Way2Force

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The mindset shift is the real takeaway for me — the org is disposable, the local project is the source of truth. Most teams I have seen treat a scratch org like a mini-sandbox and then wonder why it fills up with cruft, so your 7-day default makes sense as a forcing function. One thing I would add to the gotchas: when a scratch org expires mid-review, the test-data script becomes the whole recovery plan — how do you handle a feature that outlives a 7-day org, extend it or rebuild and re-seed? And for a trigger that needs realistic data volumes, at what point do you stop fighting the empty org and switch to the partial-copy sandbox instead?