Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Forked from cjp/delete-vpc.sh
Created December 20, 2018 22:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RichardBronosky/9e6e06b1a0c56ad983d77788fb124648 to your computer and use it in GitHub Desktop.
Save RichardBronosky/9e6e06b1a0c56ad983d77788fb124648 to your computer and use it in GitHub Desktop.
Delete AWS VPC including dependencies
#!/bin/bash -eu
#
# List all VPCs in region and delete them
main(){
if [ -z "$1" ]; then
echo "Usage: $0 <region> [<region> ...]"
exit 64
fi
for region in "$@"; do
clear_region "$region"
done
}
clear_region(){
export AWS_DEFAULT_REGION="$1"
echo "Clearing region: $AWS_DEFAULT_REGION..."
vpcs=($(aws ec2 describe-vpcs --query="Vpcs[].VpcId" --output=text))
vpcc=${#vpcs[@]}
echo "Found $vpcc VPC[s]: ${vpcs[@]}"
i=0
for vpc in "${vpcs[@]}"; do
i=$((i+1))
delete_vpc "$vpc" $i $vpcc
done
}
delete_vpc(){
wait=0
vpc=$1
echo -n "Deleting VPC $2 of $3 '$vpc'"
if [[ $wait -gt 0 ]]; then
echo -n " in $wait seconds. Press Ctrl-C to stop."
sleep $wait
fi
echo
./delete-vpc.sh $vpc
}
main "$@"
#!/bin/bash -eux
#
# Delete a VPC and its dependencies
if [ -z "$1" ]; then
echo "Usage: $0 <vpcid>"
exit 64
fi
vpcid="$1"
# Delete subnets
for i in `aws ec2 describe-subnets --filters Name=vpc-id,Values="${vpcid}" | grep subnet- | sed -E 's/^.*(subnet-[a-z0-9]+).*$/\1/'`; do
aws ec2 delete-subnet --subnet-id=$i
done
# Detach internet gateways
for i in `aws ec2 describe-internet-gateways --filters Name=attachment.vpc-id,Values="${vpcid}" | grep igw- | sed -E 's/^.*(igw-[a-z0-9]+).*$/\1/'`; do
aws ec2 detach-internet-gateway --internet-gateway-id=$i --vpc-id=${vpcid}
done
# Delete internet gateways
for i in `aws ec2 describe-internet-gateways --filters Name=attachment.vpc-id,Values="${vpcid}" | grep igw- | sed -E 's/^.*(igw-[a-z0-9]+).*$/\1/'`; do
aws ec2 delete-internet-gateway --internet-gateway-id=$i
done
# Delete security groups (ignore message about being unable to delete default security group)
for i in `aws ec2 describe-security-groups --filters Name=vpc-id,Values="${vpcid}" | grep sg- | sed -E 's/^.*(sg-[a-z0-9]+).*$/\1/' | sort | uniq`; do
aws ec2 delete-security-group --group-id $i > /dev/null 2>&1 || true
done
# Delete the VPC
aws ec2 delete-vpc --vpc-id ${vpcid}
ap-south-1
eu-north-1
ap-northeast-2
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
@RichardBronosky
Copy link
Author

Example usage:

git clone https://gist.github.com/9e6e06b1a0c56ad983d77788fb124648.git delete-vpc
cd delete-vpc
./delete-all-vpcs-in-region.sh $(cat regions.txt)

@RichardBronosky
Copy link
Author

Get all regions like so:

aws ec2 describe-regions --query='Regions[].RegionName' --output=text | tr '\t' '\n' | sort > regions.txt

DO NOT FORGET TO REMOVE REGIONS YOU WANT TO LEAVE UNTOUCHED!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment