3

We are creating scratch org from docker containers and we would need to persist the relevant files that are needed between steps. In particular, we noticed that if we authorize in a container and create a scratch org, and we authorize again in another container and list the scratch orgs, the list is empty.

How can we fetch the actual scratch org list from the dev hub itself?

1
  • 1
    Have you tried this command sfdx force:org:list to fetch all active scratch orgs? Apr 4 '19 at 14:21
8

How can we fetch the actual scratch org list from the dev hub itself?

You can query the ActiveScratchOrg object to get a list of all the active Scratch Orgs from Dev Hub.

SELECT .... 
FROM ActiveScratchOrg

If you want to get list of all Scratch Orgs created so far including active or deleted, you can utilize the ScratchOrgInfo object for that purpose.

4
  • If we find a matching scratch org that doesn't exist under the current setting, how do we re-authenticate against it? or shall we simply create a new scratch org every time? What limits apply to scratch org? Apr 4 '19 at 14:30
  • This list won't give you any inactive orgs but only active ones. So let's say even if you find one, you won't be able to re-authenticate against it as that would have been expired. In that case you will need to create a new scratch org altogether. As for the limits around scratch orgs itself, the documentation here will be a good place to start with. If you want to get list of all scratch orgs created, you can use ScratchOrgInfo
    – Jayant Das
    Apr 4 '19 at 14:34
  • Also is there a simple way to clean all scratch orgs? I think we need to clean them sometimes :) Apr 4 '19 at 15:27
  • If you are referring cleaning as deleting the orgs, then you can do so from these objects.
    – Jayant Das
    Apr 4 '19 at 15:31
5

SFDX stores authenticated org details in the ~/.sfdx folder. If you go there and look at the contents, you'll find some general logging and metadata plus one JSON file per authenticated org:

-rw-r--r--   1 d.reed  staff     60 Mar 28 17:12 sfdx-config.json
-rw-------   1 d.reed  staff     34 Mar 26 11:37 sfdx-usage.json
-rw-------   1 d.reed  staff  14314 Mar 28 16:40 sfdx.log
-rw-------   1 d.reed  staff    252 Mar 26 18:45 stash.json
-rw-------   1 d.reed  staff    784 Mar 28 17:12 test-cz5mlzy7XXXX@example.com.json
-rw-------   1 d.reed  staff    886 Mar 26 18:45 test-f9b8rjtrXXXX@example.com.json
-rw-------   1 d.reed  staff    880 Mar 26 16:00 test-gibgvu89XXXX@example.com.json

The JSON files contain the access tokens and other details of the scratch orgs.

{
    "orgId": "00D3F000000XXXXUAU",
    "accessToken": "<snipped>",
    "refreshToken": "<snipped>",
    "instanceUrl": "https://data-java-XXXX-dev-ed.cs92.my.salesforce.com/",
    "loginUrl": "https://CS92.salesforce.com",
    "username": "test-gibgvu89XXXX@example.com",
    "clientId": "PlatformCLI",
    "createdOrgInstance": "CS92",
    "created": 1553630395708,
    "devHubUsername": "you@yourorg.com"
}

You can persist this directory across containers to preserve access to created scratch orgs. Take care, though; the access and refresh tokens are the keys to the kingdom (particularly for your Dev Hub!) so make sure you only persist the data you want and ensure it's disposed appropriately.

2
  • We are trying to do that, but this will only allow you to retrieve scratch org created in the session (not ones created by other developers on the same dev hub) Apr 4 '19 at 14:30
  • What actual steps do you take, and what error or result do you see? Could you edit your question to expand on what you are currently doing?
    – David Reed
    Apr 4 '19 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.