Skip to main content

Usual Command Flows - Scratch Org Creation

Scratch orgs typically last 7-30 days. Here's the flow for creating and managing scratch orgs.

1. Create New Scratch Org

Note that the scratch org definition file must have been created before - this is usually the TA's responsability.
Bash
# Navigate to project directory
cd path/to/your/salesforce-project

# Create scratch org using project config
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch-org --duration-days 30 --set-default

2. Initial Org Setup

Bash
# Open the scratch org
sf org open --target-org my-scratch-org

# Deploy base metadata from main branch
git checkout main
git pull origin main
sf project deploy start --target-org my-scratch-org --wait 20

3. Configure Org Settings

Bash
# Assign permission sets if needed
sf org assign permset --name MyPermissionSet --target-org my-scratch-org

# Run post-setup scripts if configured
sf apex run --file scripts/setup.apex --target-org my-scratch-org

4. Create Feature Branch

Bash
# Create new branch for your work
git checkout -b feature/my-ticket-branch

# Push branch to remote
git push -u origin feature/my-ticket-branch

5. Daily Work Routine

Follow the "Daily Normal Command Flow" above for regular development work.

6. Scratch Org Renewal (Before Expiration)

Note that this will delete the scratch org, so ensure you have pulled everything before running it.
Bash
# Check org expiration date
sf org display --target-org my-scratch-org

# If org is expiring, retrieve all changes first
sf project retrieve start --target-org my-scratch-org
git add .
git commit -m "TICKET-123: Final changes before org renewal"
git push

# Delete old scratch org
sf org delete scratch --target-org my-scratch-org --no-prompt

# Create new scratch org and redeploy
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch-org-v2 --duration-days 30
sf project deploy start --target-org my-scratch-org