Salesforce DX: Useful CLI Commands

Salesforce DX is finally available and it is amazing! Now professional developers can build collaboratively with continuous delivery using Salesforce DX, the open and integrated experience that makes development on the Salesforce Platform easy.

I strongly recommend you to watch this presentation, made by Wade Wegner, with an introduction to Salesforce DX, talking about principles of Modern Software Delivery, how DX will improve the Developer Experience, some patterns and anti-patterns and how to adopt Salesforce DX:

Well, once you decide to switch, you’ll notice that you can use the Salesforce command-line interface (CLI) for most Salesforce DX tasks. These tasks include authorizing a Dev Hub org, creating a scratch org, synchronizing source code between your scratch orgs and VCS, and running tests.
You can start using the CLI right after you install it.

The CLI commands are grouped into top-level topics. For example, the force top-level topic is divided into topics that group commands by functionality, such as the force:org commands to manage your orgs.

Run —help at each level to get more information.

sfdx --help // lists all top-level topics
sfdx force --help // lists all the topics under force
sfdx force:org --help // lists all the commands in the topic force:org
sfdx force:org:open --help // detailed info about the force:org:open command

Imagine you want to login to your Hub Org:

sfdx force:auth:web:login --setalias my-hub-org --instanceurl https://login.salesforce.com

Imagine now that you want to login to your sandbox:

sfdx force:auth:web:login --setalias my-sandbox --instanceurl https://test.salesforce.com

To set this alias as your current default:

sfdx force:config:set defaultusername=my-sandbox

Maybe you want to get a list to all orgs you’re connected to:

sfdx force:org:list

To open one of these orgs:

sfdx force:org:open -u my-sandbox

To mark a scratch org for deletion:

sfdx force:org:delete -u my-sandbox


To remove an old hub org, or non-scratch org from Salesforce DX org list, you need to manually cleanup the DX configs in the local installation. It would be nice for this to be something in the actual CLI commands: the ability to clean up old orgs that are no longer needed. For the time being, use the following steps:

On Mac OS and Linux, you can find your DX config folder, called .sfdx, in your user home directory.

cd ~/.sfdx

On Windows, you can use

%USERPROFILE%\.sfdx

In that folder there are a host of .json files, named for the username of the admin user you registered for that org. In my case, in this instance it was called mgoncalves@dx.pilot, so sure enough, there it was:

mgoncalves@dx.pilot.json

Inside the file, or the hashes for current access token, refresh token, and all the other OAuth goodness that allows DX to access your org. So I simply deleted that file:

> rm mgoncalves@dx.pilot.json

That appears to have cleaned it all up.


To retrieve unpackaged Source Defined in a package.xml file (eg.: the file created by MavensMate):

sfdx force:mdapi:retrieve -r ./mdapipkg -u [your_username] -k ./package.xml

To retrieve an unmanaged package you want to convert to Salesforce DX:

sfdx force:mdapi:retrieve -s -r ./mdapipkg -u [your_username] -p

To convert the retrieved source Defined in a package.xml file: the first step is to unzip unpackaged.zip using a zip file management utility or from the command line. For example, on a Mac, double-click unpackaged.zip in Finder or type the following command on the command line:

unzip ./mdapipkg/unpackaged.zip -d ./mdapipkg/

Then convert the source code to the Salesforce DX project structure:

sfdx force:mdapi:convert -r ./mdapipkg

To create a scratch org, first create a JSON file with the or configuration. Then:

sfdx force:org:create -f project-scratch-def.json -a MyScratchOrg

To push changes, use a Force Push:

I mean, use this:

sfdx force:source:push -u MyScratchOrg

To push changes ignoring errors (with great powers, great responsibilities. Be careful):

sfdx force:source:push -u MyScratchOrg --ignorewarnings --forceoverwrite

For an even more powerful approach, you can retrieve and deploy code from/to a regular org. First, authorize the regular org using the login command we learned above. Then, choose one of the options below, depending if you want or not to deploy Lightning/Aura Components:

sfdx force:source:retrieve -m CustomObject,ApexClass,ApexTrigger,ApexPage,CustomLabels

sfdx force:source:deploy -m CustomObject,ApexClass,ApexTrigger,ApexPage,CustomLabels

sfdx force:source:deploy -m CustomObject,ApexClass,ApexTrigger,ApexPage,CustomLabels,AuraDefinitionBundle

sfdx force:source:retrieve -m CustomObject,ApexClass,ApexTrigger,ApexPage,CustomLabels,AuraDefinitionBundle

Salesforce DX CLI is a powerful command line interface, a tool that simplifies development and build automation when working with your Salesforce org.

For even more information, run this command to view all available commands in the force topic.

sfdx force:doc:commands:list

See you next time.