What You'll Learn
The 30 most valuable Salesforce CLI commands for architects and tech leads, organised by workflow: org management, source deployment, metadata inspection, Apex execution, and data operations. Use this as a reference card.
CLI Basics: The sf Command
Salesforce unified all CLI commands under the sf binary in 2022 (replacing the older sfdx binary). All new commands use sf. The old sfdx commands continue to work via an alias shim but are not updated with new features.
The --json flag on any command returns machine-readable JSON output — essential for CI/CD pipelines that parse command output.
Org Management Commands
# Authenticate to an org (opens browser OAuth flow)
sf org login web --alias myProdOrg --instance-url https://login.salesforce.com
# Authenticate to a sandbox
sf org login web --alias mySandbox --instance-url https://test.salesforce.com
# List all authenticated orgs
sf org list
# Open an org in the browser
sf org open --target-org myProdOrg
# View org limits and current usage
sf org display --target-org myProdOrg --json
# Create a scratch org
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias myScratch --duration-days 7
# Delete a scratch org
sf org delete scratch --target-org myScratch --no-prompt
# Set default org for all commands
sf config set target-org myProdOrgSource Deployment Commands
# Deploy metadata from local source to org
sf project deploy start --target-org mySandbox
# Deploy specific metadata types
sf project deploy start \
--metadata ApexClass,CustomObject,Flow \
--target-org mySandbox
# Deploy from a manifest file (package.xml)
sf project deploy start \
--manifest manifest/package.xml \
--target-org mySandbox
# Validate deployment without executing (dry-run)
sf project deploy validate \
--manifest manifest/package.xml \
--target-org myProdOrg
# Retrieve metadata from org to local
sf project retrieve start \
--metadata ApexClass:MyClass,CustomObject:Account \
--target-org mySandbox
# Quick deploy after successful validation (avoids re-running tests)
sf project deploy quick \
--job-id 0AfXX000000XXXXXX \
--target-org myProdOrgsf project deploy validate runs a full deployment simulation including Apex test execution but does not commit any changes to the org. The returned job ID can then be used with sf project deploy quick to complete the actual deployment within 10 days without re-running tests. This is the recommended pattern for production deployments.
Apex and Test Commands
# Run all Apex tests
sf apex run test --target-org mySandbox --result-format human
# Run tests for specific classes
sf apex run test \
--class-names AccountServiceTest,OrderTest \
--target-org mySandbox
# Run tests and output code coverage
sf apex run test \
--target-org mySandbox \
--code-coverage \
--result-format json \
--output-dir ./test-results
# Execute anonymous Apex
sf apex run --file scripts/apex/massUpdate.apex --target-org myProdOrg
# Execute inline anonymous Apex
echo "System.debug(Limits.getQueries());" | sf apex run --target-org myProdOrg
# Get Apex debug log (requires log level setup)
sf apex tail log --target-org mySandboxMetadata Inspection Commands
# List all metadata types in the org
sf org list metadata-types --target-org mySandbox
# List all components of a specific type
sf org list metadata \
--metadata-type ApexClass \
--target-org mySandbox
# Generate a package.xml for all metadata in org
sf project generate manifest \
--from-org mySandbox \
--output-dir manifest
# Check what changed between local and org
sf project deploy preview --target-org mySandbox
# View deployment status
sf project deploy resume --job-id 0AfXX000000XXXXXX
# Cancel a running deployment
sf project deploy cancel --job-id 0AfXX000000XXXXXX --target-org mySandboxData and SOQL Commands
# Run a SOQL query and display results
sf data query \
--query "SELECT Id, Name FROM Account LIMIT 10" \
--target-org mySandbox
# Export query results to CSV
sf data query \
--query "SELECT Id, Name, Email FROM Contact WHERE AccountId != null" \
--target-org mySandbox \
--result-format csv > contacts.csv
# Import data from CSV
sf data import tree \
--files data/Account.json \
--target-org mySandbox
# Export object data as JSON tree
sf data export tree \
--query "SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account" \
--output-dir ./data \
--target-org mySandbox
# Bulk delete records
sf data delete bulk \
--sobject Contact \
--file contacts-to-delete.csv \
--target-org mySandbox--json | jq '.result' to any CLI command to pipe the JSON output through jq for clean, formatted viewing. For Windows users, install jq via Chocolatey (choco install jq). The ability to parse CLI output in shell scripts is what enables powerful CI/CD automation.
Key Takeaways
- All new Salesforce CLI commands use
sf— the oldsfdxbinary is maintained for compatibility but not updated. --jsonon any command enables machine-readable output for CI/CD pipelines.- Validate first with
sf project deploy validate, then use quick deploy with the job ID to avoid re-running tests in production. sf apex runexecutes anonymous Apex from a file or stdin — essential for data fixes and one-off scripts.sf project generate manifestgenerates a package.xml from what's actually in the org — useful for initial source extraction.- Pipe CLI output to
jqfor clean JSON parsing in shell scripts and CI pipelines.
Check Your Understanding
1. You want to deploy to production and avoid re-running the full test suite after a successful validation. What is the two-command sequence?
2. You need to run a SOQL query and export the results to a CSV for analysis. Which command achieves this?
3. What does adding --json to a Salesforce CLI command do?
Discussion & Feedback