Last active
April 21, 2017 04:40
-
-
Save wadewegner/ac3244bb2dfbcee92190ba1d34f7f8d0 to your computer and use it in GitHub Desktop.
A script for deleting all your scratch orgs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Get default dev hub | |
configs="$(sfdx force:config:list --json)" | |
filteredConfig="$(echo ${configs} | jq -r '.results' | jq -r '.[] | select( .key | contains("defaultdevhubusername"))' | jq -r .value)" | |
# Get orgs | |
orgs="$(sfdx force:org:list --json)" | |
orgResults="$(echo ${orgs} | jq -r '.results')" | |
# check if the default dev hub is an alias or org | |
if [[ ! $filteredConfig == *"@"* ]]; then | |
# it's an alias | |
# Filter based on the alias | |
hubOrg="$(echo ${orgResults} | jq -r '.[] | select( .alias=="'$filteredConfig'")')" | |
# Get the username | |
hubOrgUsername="$(echo ${hubOrg} | jq -r .username)" | |
# Update the filteredConfig to the username | |
filteredConfig=$hubOrgUsername | |
fi | |
# Filter to scratch orgs | |
filteredOrgs="$(echo ${orgResults} | jq -r '.[] | select( .username | contains("scratch"))')" | |
# Filter to scratch orgs belonging to the default dev hub | |
filteredOrgsByHub="$(echo ${filteredOrgs} | jq -r 'select( .devHubUsername=="'$filteredConfig'")')" | |
# Get usernames | |
usernames="$(echo ${filteredOrgsByHub} | jq -r .username)" | |
for username in ${usernames[@]} | |
do | |
# Delete scratch orgs | |
sfdx force:org:delete -u $username -p | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment