Salesforce CLI
Safety-first wrapper for Salesforce CLI (sf v2). Every command is classified by risk level before execution. NEVER modify Salesforce data or metadata without explicit user permission.
When to Use
- User asks to query Salesforce data (SOQL/SOSL)
- User asks to deploy or retrieve metadata
- User asks to export/import data (CSV, JSON, tree)
- User asks to execute Apex code
- User asks to manage orgs (scratch, sandbox, production)
- User asks to authenticate w/ Salesforce
- User asks to work w/ platform events or CDC
- User asks to manage packages or permissions
- User asks to interact w/ Salesforce APIs (REST, Bulk, Tooling)
Prerequisites
- Install:
npm install -g @salesforce/cliorbrew install sf - Auth:
sf org login web(interactive) orsf org login jwt(CI/CD) - Verify:
sf --version(requires v2.x) - Default org:
sf config set target-org <alias>
Safety Model
Every sf command falls into one of four risk tiers. Default mode is read-only.
| Tier | Action Required | Examples |
|---|---|---|
| Safe | Execute immediately | sf org list, sf data query, sf project retrieve preview |
| Write | AskUserQuestion BEFORE executing | sf data create record, sf project deploy start --dry-run |
| Destructive | AskUserQuestion w/ explicit consequences | sf project deploy start, sf data delete, sf org delete |
| Forbidden | Multi-step validation, NEVER auto-confirm | Bulk delete in production, mass data wipe, production org delete |
See references/safety-rules.md for full classification and confirmation templates.
Production Guardrails
Detect org type before ANY write operation:
sf org display --target-org <alias> --json
Check instanceUrl and isScratch/isSandbox fields:
login.salesforce.comor no sandbox/scratch flag β PRODUCTION β extra confirmation requiredtest.salesforce.comorisSandbox: trueβ SandboxisScratch: trueβ Scratch org
For production orgs: ALL write operations require AskUserQuestion w/ org alias typed confirmation.
Fail-Safe Principles
- If org type detection fails β BLOCK all operations (never assume non-production)
- If unsure about tier β classify HIGHER (Write β Destructive, Destructive β Forbidden)
- Bulk operations always require row count preview before confirmation
- Deploys with
destructiveChanges.xmlare FORBIDDEN in production via scripts --test-level NoTestRunis BLOCKED for production deployments- Apex code must be shown to user BEFORE execution (never run sight-unseen)
- Permission set changes require warning about privilege escalation / lockout risks
- Field/object deletions in deploys require warning about cascade impacts (reports, flows, integrations)
- Sandbox refresh requires explicit acknowledgment that ALL data will be overwritten
- PII fields in queries trigger a warning (Email, Phone, Birthdate, SSN, etc.)
Decision Flow
Command received
β Detect target org type (prod/sandbox/scratch)
β Classify risk tier (see Quick Reference)
β Safe? Execute immediately
β Write? AskUserQuestion β wait for answer β execute or cancel
β Destructive? AskUserQuestion w/ consequences β wait β execute or cancel
β Forbidden? Warn β require typed confirmation β final confirm β execute or cancel
β PRODUCTION? Add extra confirmation step regardless of tier
Quick Reference
Safe (read-only, execute immediately)
| Command | Description |
|---|---|
sf org list | List authorized orgs |
sf org list --all | Include expired scratch orgs |
sf org display | Display org info |
sf org open | Open org in browser |
sf config list | List config values |
sf config get target-org | Get default org |
sf alias list | List aliases |
sf data query --query "..." | SOQL query (w/ mandatory LIMIT) |
sf data search --query "..." | SOSL search |
sf data get record | Get single record |
sf project retrieve preview | Preview what would be retrieved |
sf project deploy preview | Preview what would be deployed |
sf apex list log | List debug logs |
sf apex get log | Get debug log content |
sf apex tail log | Tail logs in real-time |
sf api request rest (GET) | Read-only REST API calls |
sf limits api display | Display API limits |
sf schema generate sobject | Describe object schema |
sf package list | List packages |
sf package version list | List package versions |
sf plugins list | List installed plugins |
sf doctor | Run diagnostics |
sf auth list | List auth connections |
Write (AskUserQuestion required)
| Command | Description |
|---|---|
sf data create record | Create a single record |
sf data update record | Update a single record |
sf data import tree | Import data from tree files |
sf project deploy start --dry-run | Validate deployment (no changes) |
sf project retrieve start | Retrieve metadata to local |
sf org create scratch | Create scratch org |
sf alias set | Set an alias |
sf config set | Set config value |
sf package create | Create a package |
sf package version create | Create package version |
Destructive (AskUserQuestion w/ consequences)
| Command | Description |
|---|---|
sf project deploy start (no dry-run) | Deploy metadata (modifies org) |
sf data delete record | Delete a single record |
sf data delete bulk | Bulk delete records |
sf data update bulk | Bulk update records β can corrupt data at scale |
sf data import bulk | Bulk import β can create thousands of duplicate records |
sf data upsert bulk | Bulk upsert β incorrect key can overwrite wrong records |
sf apex run | Execute anonymous Apex |
sf org delete scratch | Delete scratch org |
sf org delete sandbox | Delete sandbox |
sf org create sandbox | Create sandbox (long-running, uses resources) |
sf org assign permset | Assign permission set β can escalate privileges or lock users out |
sf package install | Install package β can modify schema, automation, and data |
sf package uninstall | Uninstall package |
sf org logout | Remove org auth |
sf api request rest -X POST/PUT/PATCH/DELETE | Write/delete REST API calls |
Forbidden (multi-step validation)
| Command | Description |
|---|---|
sf org delete targeting production | Delete production org connection |
| Bulk delete in production | Mass data deletion in production |
sf data delete bulk w/o WHERE in source query | Full-table data wipe risk |
sf project deploy start w/ destructiveChanges.xml in prod | Deploy destructive metadata changes to production |
sf project deploy start --test-level NoTestRun in prod | Skip tests in production (BLOCKED) |
sf org refresh sandbox | Refresh sandbox β overwrites ALL data and customizations |
sf org logout --all | Remove ALL org auth β can break CI/CD pipelines |
| Bulk loops w/o limits | Any loop running delete/update w/o LIMIT |
sf apex run in production w/o review | Execute unreviewed Apex in production |
| Dangerous deploy flags in prod | --ignore-conflicts, --ignore-warnings, --purge-on-delete |
SOQL/SOSL Query Safety
Mandatory rules for ALL queries:
- Always add LIMIT β Default to
LIMIT 200if user omits - Never use
SELECT *β Expand fields viasf schema generate sobjector ask user - Production queries β Require WHERE clause unless object is known to be small
- Tooling API queries β Use
--use-tooling-apiflag for metadata queries
# Safe SOQL query
sf data quer