55

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
  • 7
    Is that your music collection hiding in the back of the screenshots? Aug 3, 2017 at 1:29
  • 2
    Uh...wow! Good eye. LOL.
    – pchittum
    Aug 3, 2017 at 16:05

6 Answers 6

61

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 peter@dx.pilot, so sure enough, there it was:

peter@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 peter@dx.pilot.json

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

3
  • I believe SalesforceDX is still in beta state, so I would suggest not to use for big projects. Jul 4, 2017 at 18:05
  • To windows go to path like "C:\Users\ [MyUserName] \.sfdx" and you can to see the files 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
    Jun 22, 2021 at 23:04
56

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.

6
  • 1
    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
    Apr 16, 2018 at 22:05
  • 1
    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
    Aug 20, 2018 at 7:02
  • 1
    That 30 day expire on trial hub orgs is a real PITA Sep 25, 2018 at 7:34
  • 1
    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
    Dec 17, 2018 at 15:09
  • 1
    This is the correct answer. Thanks for this.
    – Arthlete
    Jul 29, 2019 at 20:10
21
D:\...>sfdx force:org:list
=== Orgs
     ALIAS         USERNAME                                    ORG ID              CONNECTED STATUS
───  ────────────  ──────────────────────────────────────────  ──────────────────  ────────────────
     DevHub        daniel.ballinger+devhub@example.com           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
    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
    Jul 16, 2018 at 10:21
  • Try adding a new valid scratch org first 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 defaultdevhubusername=mynewdevhub@username.com --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.

1

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>

0

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"

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.