67

In the Salesforce DX pilot, every pilot participant was given a temporary dev hub org. These orgs expired, were destroyed, and sent back to the matrix in advance of the open beta.

I had an existing installation of the Salesforce DX cli, and I found after upgrading, my old dev hub was still hanging about showing it had an invalid grant. So time to do some tidying.

enter image description here

Normally, to delete a scratch org, you'd just delete it. I wasn't sure, so first I attempted to remove it that way:

sfdx force:org:delete -u dev-hub

Only this didn't work. Salesforce DX only allows the deletion of scratch orgs, not other org types.

I then created my new dev hub and attempted to replace the existing dev-hub alias by doing...

sfdx force:auth:web:login -a dev-hub -d

Only I was left with it still registered in the list, just with no more alias.

enter image description here

How do you remove a dev-hub org, or any other non-scratch org from the list of orgs in the SFDX CLI?

2
  • 8
    Is that your music collection hiding in the back of the screenshots? Commented Aug 3, 2017 at 1:29
  • 3
    Uh...wow! Good eye. LOL.
    – pchittum
    Commented Aug 3, 2017 at 16:05

9 Answers 9

3

With the retirement of the sfdx command, using the new sf CLI command style, this is now accomplished with the org logout command:

> sf org logout --target-org <ALIAS_NAME> --no-prompt
84

This hackerific set of steps may still work, but my wish to have an actual CLI command was granted years ago, and now lives on in the new sf command incantations. See the answer here.


Original Answer (for posterity)

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, you need to manually cleanup the DX configs in the local installation.

On mac/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 [email protected], so sure enough, there it was:

[email protected]

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 [email protected]

That appears to have cleaned it all up, and I'm no longer bothered by the "invalid grant" message and the shadow org.

4
  • I believe SalesforceDX is still in beta state, so I would suggest not to use for big projects. Commented Jul 4, 2017 at 18:05
  • 1
    To windows go to path like "C:\Users\ [MyUserName] \.sfdx" and you can to see the files Commented Jan 28, 2021 at 16:45
  • Thank you so much, this was driving me crazy trying to find the json files where the Org Aliases and auth tokens were stored. For anybody else dealing with this issue, and you're able to authorize an org / set an org as default (ie- open the org), but cannot deploy/retrieve, try restarting VS Code. I think I was dealing with stale data cached in my system and this helped immediately.
    – Sam Howle
    Commented Jun 22, 2021 at 23:04
  • I cleaned up the aliases JSON before deleting the OAuth files. Not sure if that is automatic or not, but obviously want those to be removed from the list if the logins are removed. Commented Oct 24, 2022 at 17:24
71

It looks like rather than deleting a hub org, you just call:

sfdx force:auth:logout -u <hub alias>

You'll get a warning about being sure you want to log out. Once you do, that hub org will no longer show up in the list.

5
  • 2
    Only downside is that if that was your default, you're left without a default hub if that was your default. Use sfdx force:config:set defaultdevhubusername=<devhubusername> --global to repair
    – Dave Lyon
    Commented Apr 16, 2018 at 22:05
  • 2
    Just wanted to note that this answer is particularly helpful if the org being removed is a non-hub, non-scratch org, that we probably don't want to delete, but only remove from the org list.
    – SamuelDev
    Commented Aug 20, 2018 at 7:02
  • 1
    That 30 day expire on trial hub orgs is a real PITA Commented Sep 25, 2018 at 7:34
  • 2
    This worked for me, as I needed to update the sandbox alias to a new sandbox. Just did the force:auth:logout, then a new force:auth:web:login after that. Thanks.
    – specimen
    Commented Dec 17, 2018 at 15:09
  • ✅ THIS IS THE CORRECT ANSWER!
    – Bahman.A
    Commented Jun 3, 2021 at 17:26
22
D:\...>sfdx force:org:list
=== Orgs
     ALIAS         USERNAME                                    ORG ID              CONNECTED STATUS
───  ────────────  ──────────────────────────────────────────  ──────────────────  ────────────────
     DevHub        [email protected]           00D7F0000000001AAA  invalid_grant

I just had my trial Dev Hub org expire (invalid_grant), so I created a new Dev Hub and registered it with sfdx force:auth:web:login -d.

That moved the (D) marker to my new dev hub, but the old one was still hanging around. Peter's solution would have worked, but I thought I'd try a sfdx force:org:delete -u DevHub to see what happens now the old org is no longer the nominated dev hub.

D:\...>sfdx force:org:delete -u DevHub
Enqueue scratch org with name: DevHub for deletion?  Are you sure (y/n)?: y
Successfully deleted scratch org DevHub.

Appears to have worked even though it isn't a scratch org. The old dev hub org no longer appears for a sfdx force:org:list

3
  • much simpler than the accepted answer, just use the email address (username) if there's no alias.
    – Aequitas
    Commented Feb 26, 2018 at 23:39
  • 1
    My devhub scratch org is expired and I can't delete it using delete command. Receive an error "The Dev Hub org cannot be deleted." when I try to do that $ sfdx force:org:delete -u DevHub Enqueue scratch org with name: DevHub for deletion? Are you sure (y/n)?: y ERROR: The Dev Hub org cannot be deleted.
    – Patlatus
    Commented Jul 16, 2018 at 10:21
  • Try adding a new valid scratch org first Commented Jul 16, 2018 at 10:26
6

I just ran into this issue while trying to create a scratch org and found a working solution. After I authorized my production DevHub, entering the following allowed me to remove the old TrialDevHub org with the invalid_grant.

~ sfdx force:config:set [email protected] --global

...

~ sfdx force:org:delete -u TrialDevHub
Enqueue scratch org with name: TrialDevHub for deletion?  Are you sure (y/n)?: y
Successfully deleted scratch org TrialDevHub.

sfdx force:org:list no longer shows my expired TrialDevHub and I can successfully create scratch orgs once again.

This worked using sfdx-cli version 6.0.10-3713d7b on a Mac with node version 8.6.0.

4

Newest sfdx command line:

  • sfdx org list
  • sfdx org logout -o org_alias
3

Try using sfdx force:org:list --clean

this will clear inactive or deleted orgs scratch orgs only from sfdx

for non scratch org clean inside folder "C:\Users\ ~UserName~ .sfdx"

2

To keep the dev hub and scratch organization always under your management,

use sfdx force:org:list for full list of current dev hub and scratch org

use sfdx force:org:delete -u <the alias with scratch org which you can find from above list> to delete the selected scratch

use sfdx force:config:set defaultdevhubusername=<the username as new default dev hub> to assign new default dev hub

Only after you have new dev hub as default, can you proceed for removing existing dev hub by using sfdx force:org:delete -u <the alias with exisitng dev hub org which you can find from above list>

1

Use this SFDX CMD

>sfdx force:logout -o <ALIAS_NAME>

CLI Version:7.209.6 & above.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .