Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Created August 30, 2019 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save douglascayers/bd37e492ca410657696b82a42c4c6702 to your computer and use it in GitHub Desktop.
Save douglascayers/bd37e492ca410657696b82a42c4c6702 to your computer and use it in GitHub Desktop.
Deletes a scratch org when `force:org:delete` fails for any reason
org_list_json=$(sfdx force:org:list --json)
scratch_org_username=$1
devhub_username=$2
# if no scratch org username given, use default
if [ -z "$1" ]; then
scratch_org_username=$(echo $org_list_json | jq -r '.result.scratchOrgs[] | select(.isDefaultUsername) | .signupUsername')
else
# else check if argument is an org alias and get signup username from that
scratch_org_username=$(echo $org_list_json | jq -r --arg ALIAS "$1" '.result.scratchOrgs[] | select(.alias==$ALIAS) | .signupUsername')
# else check if argument is an org username and get signup username from that
if [ -z "$scratch_org_username" ]; then
scratch_org_username=$(echo $org_list_json | jq -r --arg USERNAME "$1" '.result.scratchOrgs[] | select(.username==$USERNAME) | .signupUsername')
fi
fi
echo "scratch_org_username=$scratch_org_username"
# if no devhub username given, use default
if [ -z "$2" ]; then
devhub_username=$(echo $org_list_json | jq -r '.result.nonScratchOrgs[] | select(.isDefaultDevHubUsername) | .username')
else
# else check if argument is an org alias and get devhub username from that
devhub_username=$(echo $org_list_json | jq -r --arg ALIAS "$2" '.result.nonScratchOrgs[] | select(.alias==$ALIAS and .isDevHub) | .username')
# else check if argument is an org username and get devhub username from that
if [ -z "$devhub_username" ]; then
devhub_username=$(echo $org_list_json | jq -r --arg USERNAME "$2" '.result.nonScratchOrgs[] | select(.username==$USERNAME and .isDevHub) | .username')
fi
fi
echo "devhub_username=$devhub_username"
# exit if no scratch org username
if [ -z "$scratch_org_username" ]; then
echo "ERROR: Could not determine scratch org username to delete"
exit 1
fi
# exit if no devhub username
if [ -z "$devhub_username" ]; then
echo "ERROR: Could not determine dev hub to delete scratch org from"
exit 1
fi
# delete scratch org info record which will delete the scratch org itself
sfdx force:data:record:delete --sobjecttype ScratchOrgInfo --targetusername $devhub_username --where "SignupUsername='$scratch_org_username'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment