# 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

<div class="codehilite" id="bkmrk-note-that-the-scratc"><div class="code-buttons" data-partial-update-ignore="true">Note that the scratch org definition file must have been created before - this is usually the TA's responsability.</div></div><div class="codehilite" id="bkmrk-bash"><span class="filename">Bash</span></div>```
# 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

<div class="codehilite" id="bkmrk-bash-1"><span class="filename">Bash</span></div>```
# 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

<div class="codehilite" id="bkmrk-bash-2"><span class="filename">Bash</span></div>```
# 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

<div class="codehilite" id="bkmrk-bash-3"><span class="filename">Bash</span></div>```
# 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)

<div class="codehilite" id="bkmrk-note-that-this-will-"><div class="code-buttons" data-partial-update-ignore="true">Note that this will delete the scratch org, so ensure you have pulled everything before running it.</div><div class="code-buttons" data-partial-update-ignore="true"></div></div><div class="codehilite" id="bkmrk-bash-4"><span class="filename">Bash</span></div>```
# 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



```

---