Sub topic 2
What a Dev Hub does
This chapter explains the role of a Dev Hub org and how it relates to the scratch orgs it creates. It assumes you know what a Salesforce org is, can navigate Setup, and have the sf CLI installed. You don't need to enable anything yet. Later chapters cover enabling Dev Hub and authorizing it.
The short version
A Dev Hub is an ordinary Salesforce org with the Dev Hub feature turned on. Once the feature is on, the org can do three things:
- Create scratch orgs on request.
- Keep a record of every scratch org it has created.
- Enforce limits on how many scratch orgs exist.
A scratch org doesn't come from nowhere. Each one is created on behalf of a specific Dev Hub, and it stays linked to that Dev Hub until it is deleted or expires.
Dev Hub as a control plane
It helps to separate where you work from where you manage. You build and test in a scratch org. The Dev Hub only tracks scratch orgs and gates their creation. Developers normally don't build features in the Dev Hub itself.
The Dev Hub does not hold your code or your configuration. Your project's source lives in version control. The scratch org holds a deployed copy of that source while you work.
The Dev Hub holds:
- A record for each scratch org request.
- A record for each scratch org that is still alive.
- Allocation counters that decide whether the next request is allowed.
- Other Salesforce DX data that depends on a Dev Hub, such as namespace registrations and second-generation package data.
What happens when you create a scratch org
When you run sf org create scratch, the CLI does not talk to a template library. It authenticates to your Dev Hub, sends your definition file, and asks the Dev Hub to provision a new org. The sequence below shows the exchange.
The important points in this flow:
- The limit check happens before provisioning. If you are over a limit, the request fails at the Dev Hub, and no scratch org is created.
- The request is recorded in the Dev Hub. That record is how the Dev Hub knows the org exists and when it expires.
- The CLI stores the connection details on your machine, which lets later commands use
--target-orgwith an alias.
The records a Dev Hub keeps
Two Dev Hub objects matter most day to day:
| Object | What it represents | Typical use |
|---|---|---|
| `ScratchOrgInfo` | One record per scratch org creation request, including its status and expiration. | Auditing who created what, and finding failed requests. |
| `ActiveScratchOrg` | One record per scratch org that currently exists. | Counting active orgs and finding ones to clean up. |
Because these are normal objects, you can query them with the CLI:
sf data query --query "SELECT Id, OrgName, Status, ExpirationDate, SignupUsername FROM ScratchOrgInfo ORDER BY CreatedDate DESC LIMIT 10" --target-org my-hubThis returns the ten most recent creation requests. It is a quick way to see whether a request failed, who the signup username was, and when each org expires.
The ScratchOrgInfo record is also the underlying mechanism of creation. The CLI command is a convenient wrapper around creating a ScratchOrgInfo record and waiting for the org to be provisioned.
The link between a Dev Hub and its scratch orgs
Each scratch org is tied to the Dev Hub that created it. This link has practical consequences:
- Expiry. The Dev Hub tracks the expiration date. When the date passes, the scratch org is deleted. The default lifetime is 7 days, and the maximum is 30 days.
- Counting. Active scratch orgs count against the Dev Hub's active limit until they are deleted or expire.
- Cleanup. You can delete a scratch org from the CLI, which frees its slot in the Dev Hub.
- Authorization. To create new scratch orgs, you must be authorized to the Dev Hub. Having authorized only an old scratch org is not enough.
Other things a Dev Hub is used for
Scratch orgs are the main reason people enable Dev Hub, but the same feature supports other Salesforce DX capabilities:
- Second-generation packaging. You create and promote package versions from a Dev Hub. Package creation commands target it.
- Namespace registries. A namespace you link to a Dev Hub is registered there, and scratch orgs can use it.
- Org shape. A shape captures the settings and features of an existing org so scratch orgs can imitate it.
- Scratch org snapshots. A snapshot saves a prepared scratch org so you can create new ones from it faster.
You will meet org shape and snapshots in a later chapter. For now, treat them as extensions of the same idea: the Dev Hub stores the information that lets Salesforce build orgs on demand.
Choosing which org should be your Dev Hub
You don't need a separate org for every project. A team normally shares one Dev Hub, and every developer authorizes to it. Some points to weigh:
- Shared allocation. All developers on a shared Dev Hub draw from the same limits. A busy team can hit the daily limit if everyone creates orgs often.
- Separation from production data. Enabling Dev Hub in a production org is allowed, and many teams do it. Even so, scratch orgs do not copy production data. Creating them does not touch your customers' records.
- Permissions. People who create scratch orgs need access to the Dev Hub. A later chapter covers which permissions are required.
- Not reversible. Once you enable Dev Hub in an org, you cannot turn it off. Choose the org deliberately.
Common misunderstandings
- "The Dev Hub is where I develop." It is not. Treat it as the place that creates and tracks scratch orgs. Develop in a scratch org, and keep source in version control.
- "A scratch org copies my production org." It does not. A scratch org is built from your definition file, so it starts nearly empty. Org shape is the feature that lets it mirror an existing org's settings.
- "Deleting a scratch org deletes the Dev Hub record." The org is removed and its active slot is freed, but the Dev Hub keeps the history of the request. That is why
ScratchOrgInfocan show orgs that no longer exist. - "The alias is stored in the Dev Hub." Aliases live in your local CLI configuration. Another developer using the same Dev Hub will not see yours.
Checking your understanding
Use this model in later chapters. When something goes wrong creating a scratch org, ask where the failure sits. It may be that you are not authorized to the Dev Hub, the Dev Hub refused the request because of a limit, or the definition file asked for something Salesforce could not provision. Those three places match the three roles a Dev Hub plays: identity, allocation, and provisioning.
Exercise
A teammate says, "I deleted my scratch org yesterday, so it can't appear in the Dev Hub anymore." Name the Dev Hub object that would still show the creation request, and the one that would no longer list the org as alive.
Show answer
The ScratchOrgInfo object still holds the creation request, including its status and expiration date. The ActiveScratchOrg object no longer lists the org, because that object only contains scratch orgs that currently exist. Deleting the org frees its slot against the active limit, but the request history remains.
Exercise
You run sf org create scratch and the Dev Hub rejects it because a limit was reached. Was the scratch org provisioned before the rejection? Explain using the order of steps.
Show answer
No. The Dev Hub checks the active and daily limits before it provisions the org, so a rejected request produces no scratch org. The failure occurs at the Dev Hub, which is why you would check the limits there rather than debug the scratch org.