Skip to content

Instantly share code, notes, and snippets.

@chirauki
Last active September 3, 2025 15:36
Show Gist options
  • Select an option

  • Save chirauki/a1377b9347ea9b17516fab37bd239a04 to your computer and use it in GitHub Desktop.

Select an option

Save chirauki/a1377b9347ea9b17516fab37bd239a04 to your computer and use it in GitHub Desktop.
Openshit install cleanup
aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125-ocp-patch*' | jq -r '.Vpcs[] | .VpcId'
Terminate all instances:
aws ec2 describe-instances --page-size 400 --filters 'Name=tag:Name,Values=*rc4-1125*' | jq '.Reservations[] | .Instances[] | .InstanceId' | xargs aws ec2 terminate-instances --instance-ids
Delete all LBs:
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for arn in $(aws elbv2 describe-load-balancers --page-size 400 --query 'LoadBalancers[?Type == `network`]' | jq -r --arg VPC ${vpc} '.[] | select(.VpcId==$VPC) | .LoadBalancerArn'); do aws elbv2 delete-load-balancer --load-balancer-arn ${arn} ; done ; done
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for name in $(aws elb describe-load-balancers --page-size 400 | jq -r --arg VPC ${vpc} '.LoadBalancerDescriptions[] | select(.VPCId==$VPC) | .LoadBalancerName'); do aws elb delete-load-balancer --load-balancer-name ${name} ; done ; done
Delete all nat gateways:
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for nat in $(aws ec2 describe-nat-gateways --page-size 400 --filter Name=vpc-id,Values=${vpc} | jq -r '.NatGateways[] | .NatGatewayId'); do aws ec2 delete-nat-gateway --nat-gateway-id ${nat}; done ; done
Delete network interfaces:
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for int in $(aws ec2 describe-network-interfaces --page-size 400 --filters Name=vpc-id,Values=${vpc} | jq -r '.NetworkInterfaces[] | select(has("Attachment") | not) | .NetworkInterfaceId'); do aws ec2 delete-network-interface --network-interface-id ${int}; done ; done
Delete all subnets:
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for subnet in $(aws ec2 describe-subnets --filters Name=vpc-id,Values=${vpc} | jq -r '.Subnets[] | .SubnetId'); do aws ec2 delete-subnet --subnet-id ${subnet}; done; done
Delete all route tables:
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for rtb in $(aws ec2 describe-route-tables --filters Name=vpc-id,Values=${vpc} | jq -r '.RouteTables[] | select(.Associations == []) | .RouteTableId' ); do aws ec2 delete-route-table --route-table-id ${rtb}; done ; done
Delete all internet gateways:
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for igw in $(aws ec2 describe-internet-gateways --filters Name=attachment.vpc-id,Values=${vpc} | jq -r '.InternetGateways[] | .InternetGatewayId'); do aws ec2 detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}; aws ec2 delete-internet-gateway --internet-gateway-id ${igw} ; done; done
Delete all security groups:
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for sg in $(aws ec2 describe-security-groups --filters Name=vpc-id,Values=${vpc} | jq -r '.SecurityGroups[] | .GroupId'); do aws ec2 revoke-security-group-ingress --group-id ${sg} --ip-permissions "$(aws ec2 describe-security-groups --group-ids ${sg} | jq -r '.SecurityGroups[] | .IpPermissions')"; aws ec2 revoke-security-group-ingress --group-id ${sg} --ip-permissions "$(aws ec2 describe-security-groups --group-ids ${sg} | jq -r '.SecurityGroups[] | .IpPermissionsEgress')" ; aws ec2 delete-security-group --group-id ${sg}; done ; done
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for sg in $(aws ec2 describe-security-groups --filters Name=vpc-id,Values=${vpc} | jq -r '.SecurityGroups[] | .GroupId'); do aws ec2 delete-security-group --group-id ${sg}; done ; done
Delete vpc endpoints:
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do for vpce in $(aws ec2 describe-vpc-endpoints --filters Name=vpc-id,Values=${vpc} | jq -r '.VpcEndpoints[] | .VpcEndpointId'); do aws ec2 delete-vpc-endpoints --vpc-endpoint-ids ${vpce}; done; done
Delete VPC:
for vpc in $(aws ec2 describe-vpcs --filters 'Name=tag:Name,Values=*rc4-1125*' | jq -r '.Vpcs[] | .VpcId') ; do aws ec2 delete-vpc --vpc-id ${vpc}; done
Route53:
Delete resource record sets:
for zone in $( aws route53 list-hosted-zones | jq -r '.HostedZones[] | select((.Name | startswith("pooled-")) or (.Name | startswith("upgrade-"))) | .Id' |awk -F'/' '{print $NF}' ); do aws route53 change-resource-record-sets --hosted-zone-id ${zone} --change-batch "$(jq -n --argjson CHANGES "$(aws route53 list-resource-record-sets --hosted-zone-id ${zone} | jq -r '[.ResourceRecordSets[] | select((.Type=="A")or(.Type=="TXT")) | {"Action": "DELETE", "ResourceRecordSet": .}]')" '{"Changes": $CHANGES }')" ; done
Delete hosted zones:
for zone in $( aws route53 list-hosted-zones | jq -r '.HostedZones[] | select((.Name | startswith("pooled-")) or (.Name | startswith("upgrade-"))) | .Id' |awk -F'/' '{print $NF}' ); do aws route53 delete-hosted-zone --id ${zone} ; done
S3:
Empty and delete S3 buckets:
for bucket in $(aws s3 ls | awk '{print $NF}'); do aws s3 rm s3://${bucket} --recursive > /dev/null ; done
for bucket in $(aws s3 ls | awk '{print $NF}'); do aws s3api delete-bucket --bucket ${bucket}; done
IAM:
for user in $(aws iam list-users | jq -r '.Users[] | .UserName'); do for k in $(aws iam list-access-keys --user-name ${user} | jq -r '.AccessKeyMetadata[] | .AccessKeyId'); do aws iam delete-access-key --user-name ${user} --access-key-id ${k} ; done ; done
for user in $(aws iam list-users | jq -r '.Users[] | .UserName'); do for pol in $(aws iam list-attached-user-policies --user-name ${user} | jq -r '.AttachedPolicies[] | .PolicyArn'); do aws iam detach-user-policy --user-name ${user} --policy-arn "${pol}"; done; done
for i in $(aws iam list-instance-profiles | jq -r '.InstanceProfiles[] | .InstanceProfileName'); do aws iam delete-instance-profile --instance-profile-name ${i}; done
for pol in $(aws iam list-policies --scope Local | jq -r '.Policies[] | select( (.PolicyName | startswith("pooled-")) or (.PolicyName | startswith("upgrade-")) or (.PolicyName | startswith("vm-test-")) ) | .Arn'); do aws iam delete-policy --policy-arn ${pol} ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment