Skip to content

Instantly share code, notes, and snippets.

@jakefhyde
Last active February 20, 2024 20:21
Show Gist options
  • Select an option

  • Save jakefhyde/ecdbc7b196bb589b2bed45e5fad322ce to your computer and use it in GitHub Desktop.

Select an option

Save jakefhyde/ecdbc7b196bb589b2bed45e5fad322ce to your computer and use it in GitHub Desktop.
Clean up Digital Ocean Droplets, Domains, and SSH Keys
#!/usr/bin/zsh
echo "Deleting droplets"
name=$1
while read -r entry;
do
if [[ -z $(echo $entry | xargs) ]];
then
break
fi
droplet_id="$(echo $entry | awk '{print $1}')"
droplet_name="$(echo $entry | awk '{print $2}')"
echo "Deleting droplet $droplet_name with id:\t$droplet_id"
doctl compute droplet delete $droplet_id -f
echo
done <<<"$(doctl compute droplet list --format 'ID, Name' | grep $name)"
echo "Deleting ssh-keys"
while read -r entry;
do
if [[ -z $(echo $entry | xargs) ]];
then
break
fi
sshkey_id="$(echo $entry | awk '{print $1}')"
sshkey_name="$(echo $entry | awk '{print $2}')"
echo "Deleting ssh-key $sshkey_name with id:\t$sshkey_id"
doctl compute ssh-key delete $sshkey_id -f
echo
done <<<"$(doctl compute ssh-key list | grep $name)"
echo "Deleting dns records"
while read entry;
do
if [[ -z $(echo $entry | xargs) ]];
then
break
fi
record_id="$(echo $entry | awk '{print $1}')"
record_name="$(echo $entry | awk '{print $3}')"
echo "Deleting dns record $record_name with id:\t$record_id"
doctl compute domain records delete cp-dev.rancher.space $record_id -f
echo
done <<<"$(doctl compute domain records list cp-dev.rancher.space | grep $name)"
echo "Deleting loadbalancers"
while read entry;
do
if [[ -z $(echo $entry | xargs) ]];
then
break
fi
record_id="$(echo $entry | awk '{print $1}')"
record_name="$(echo $entry | awk '{print $3}')"
echo "Deleting loadbalancer $record_name with id:\t$record_id"
doctl compute load-balancer delete $record_id -f
echo
done <<<"$(doctl compute load-balancer list | grep $name)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment