Skip to content

Commit 5c75864

Browse files
committed
fix cleanup script
1 parent 91fa11d commit 5c75864

File tree

1 file changed

+25
-92
lines changed

1 file changed

+25
-92
lines changed

scripts/cleanup-all.sh

Lines changed: 25 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# DevOps The Hard Way - Azure - Cleanup Script
4-
# This script destroys all resources created by the deployment
4+
# This script deletes the resource groups which removes all resources
55

66
set -e # Exit on any error
77

@@ -13,147 +13,80 @@ BLUE='\033[0;34m'
1313
NC='\033[0m' # No Color
1414

1515
# Configuration
16-
PROJECT_NAME="${PROJECT_NAME:-devopsthehardwayjuly25}"
16+
PROJECT_NAME="${PROJECT_NAME:-devopsthehardway}"
1717
LOCATION="${LOCATION:-uksouth}"
1818
RESOURCE_GROUP="${PROJECT_NAME}-rg"
1919
TERRAFORM_RG="${PROJECT_NAME}-terraform-rg"
2020

2121
echo -e "${RED}🗑️ DevOps The Hard Way - Azure - CLEANUP${NC}"
22-
echo -e "${RED}⚠️ WARNING: This will DELETE ALL resources created by this project!${NC}"
22+
echo -e "${RED}⚠️ WARNING: This will DELETE ALL resources by removing resource groups!${NC}"
2323
echo -e "${YELLOW}Project: ${PROJECT_NAME}${NC}"
24-
echo -e "${YELLOW}Location: ${LOCATION}${NC}"
24+
echo -e "${YELLOW}Resource Groups to Delete:${NC}"
25+
echo -e "${YELLOW}${RESOURCE_GROUP}${NC}"
26+
echo -e "${YELLOW}${PROJECT_NAME}-node-rg${NC}"
27+
echo -e "${YELLOW}${TERRAFORM_RG} (optional)${NC}"
2528
echo ""
2629

2730
# Confirmation prompt
28-
read -p "Are you sure you want to delete all resources? Type 'DELETE' to confirm: " confirmation
31+
read -p "Are you sure you want to delete all resource groups? Type 'DELETE' to confirm: " confirmation
2932
if [ "$confirmation" != "DELETE" ]; then
3033
echo -e "${GREEN}✅ Cleanup cancelled${NC}"
3134
exit 0
3235
fi
3336

3437
echo ""
35-
echo -e "${RED}🗑️ Starting cleanup process...${NC}"
36-
37-
# Function to print step headers
38-
print_step() {
39-
echo -e "${YELLOW}📋 Step $1: $2${NC}"
40-
echo "----------------------------------------"
41-
}
38+
echo -e "${RED}🗑️ Starting resource group deletion...${NC}"
4239

4340
# Check if logged into Azure
4441
if ! az account show &> /dev/null; then
4542
echo -e "${RED}❌ Not logged into Azure. Please login first.${NC}"
4643
az login
4744
fi
4845

49-
print_step "1" "Cleaning up Kubernetes resources"
50-
if kubectl config current-context &> /dev/null; then
51-
echo -e "${YELLOW}📋 Deleting application deployment...${NC}"
52-
kubectl delete -f ../4-kubernetes_manifest/deployment.yml --ignore-not-found=true
53-
54-
echo -e "${YELLOW}📋 Deleting gateway resources...${NC}"
55-
kubectl delete gateway gateway-01 -n thomasthorntoncloud --ignore-not-found=true
56-
kubectl delete httproute traffic-thomasthorntoncloud -n thomasthorntoncloud --ignore-not-found=true
57-
58-
echo -e "${YELLOW}📋 Deleting ALB Controller...${NC}"
59-
helm uninstall alb-controller -n azure-alb-system --ignore-not-found
60-
kubectl delete namespace azure-alb-system --ignore-not-found=true
61-
kubectl delete namespace thomasthorntoncloud --ignore-not-found=true
62-
63-
echo -e "${GREEN}✅ Kubernetes resources cleaned up${NC}"
64-
else
65-
echo -e "${YELLOW}⚠️ No Kubernetes context found, skipping Kubernetes cleanup${NC}"
66-
fi
67-
echo ""
68-
69-
print_step "2" "Cleaning up Resource Groups (All Infrastructure)"
70-
echo -e "${BLUE}ℹ️ Deleting resource groups will remove ALL resources including Terraform-managed infrastructure${NC}"
71-
echo -e "${YELLOW}📋 Deleting main resource group...${NC}"
46+
echo -e "${YELLOW}📋 Deleting main resource group: ${RESOURCE_GROUP}${NC}"
7247
az group delete --name "$RESOURCE_GROUP" --yes --no-wait 2>/dev/null || echo -e "${YELLOW}⚠️ Resource group may not exist${NC}"
7348

74-
echo -e "${YELLOW}📋 Deleting AKS node resource group...${NC}"
49+
echo -e "${YELLOW}📋 Deleting AKS node resource group: ${PROJECT_NAME}-node-rg${NC}"
7550
az group delete --name "${PROJECT_NAME}-node-rg" --yes --no-wait 2>/dev/null || echo -e "${YELLOW}⚠️ Node resource group may not exist${NC}"
7651

52+
# Optional: Delete Terraform state storage
7753
echo ""
78-
79-
print_step "3" "Cleaning up Terraform State Files (Optional)"
80-
echo -e "${YELLOW}⚠️ Do you want to clean up local Terraform state files?${NC}"
81-
echo -e "${YELLOW}This will remove .terraform directories and state files from all modules${NC}"
82-
read -p "Clean up Terraform state files locally? (y/N): " clean_tf_state
83-
84-
if [[ "$clean_tf_state" =~ ^[Yy]$ ]]; then
85-
echo -e "${YELLOW}📋 Cleaning up Terraform state files...${NC}"
86-
find ../2-Terraform-AZURE-Services-Creation -name ".terraform" -type d -exec rm -rf {} + 2>/dev/null || true
87-
find ../2-Terraform-AZURE-Services-Creation -name "terraform.tfstate*" -type f -delete 2>/dev/null || true
88-
find ../2-Terraform-AZURE-Services-Creation -name "tfplan" -type f -delete 2>/dev/null || true
89-
echo -e "${GREEN}✅ Local Terraform state files cleaned up${NC}"
90-
else
91-
echo -e "${BLUE}ℹ️ Local Terraform state files preserved${NC}"
92-
fi
93-
94-
echo ""
95-
96-
print_step "4" "Cleaning up Terraform Remote State Storage (Optional)"
9754
echo -e "${YELLOW}⚠️ Do you want to delete the Terraform state storage as well?${NC}"
98-
echo -e "${YELLOW}This will remove: ${TERRAFORM_RG} resource group and ${PROJECT_NAME}tfstate storage account${NC}"
55+
echo -e "${YELLOW}This will remove: ${TERRAFORM_RG}${NC}"
9956
read -p "Delete Terraform state storage? (y/N): " delete_state
10057

10158
if [[ "$delete_state" =~ ^[Yy]$ ]]; then
102-
echo -e "${YELLOW}📋 Deleting Terraform state storage...${NC}"
59+
echo -e "${YELLOW}📋 Deleting Terraform state resource group: ${TERRAFORM_RG}${NC}"
10360
az group delete --name "$TERRAFORM_RG" --yes --no-wait 2>/dev/null || echo -e "${YELLOW}⚠️ Terraform state resource group may not exist${NC}"
10461
echo -e "${GREEN}✅ Terraform state storage cleanup initiated${NC}"
10562
else
10663
echo -e "${BLUE}ℹ️ Terraform state storage preserved${NC}"
10764
fi
10865

10966
echo ""
110-
111-
print_step "5" "Cleaning up Local Docker Images (Optional)"
112-
echo -e "${YELLOW}⚠️ Do you want to clean up local Docker images?${NC}"
113-
read -p "Remove local Docker images for this project? (y/N): " clean_docker
114-
115-
if [[ "$clean_docker" =~ ^[Yy]$ ]]; then
116-
echo -e "${YELLOW}📋 Removing local Docker images...${NC}"
117-
docker rmi "${PROJECT_NAME}azurecr.azurecr.io/thomasthorntoncloud:v2" 2>/dev/null || echo -e "${YELLOW}⚠️ Image may not exist locally${NC}"
118-
docker rmi "${PROJECT_NAME}azurecr.azurecr.io/thomasthorntoncloud:v1" 2>/dev/null || echo -e "${YELLOW}⚠️ Image may not exist locally${NC}"
119-
echo -e "${GREEN}✅ Local Docker images cleaned up${NC}"
120-
else
121-
echo -e "${BLUE}ℹ️ Local Docker images preserved${NC}"
122-
fi
123-
124-
echo ""
125-
126-
print_step "6" "Cleanup Summary"
127-
echo -e "${GREEN}✅ Cleanup process completed!${NC}"
67+
echo -e "${GREEN}✅ Resource group deletion initiated!${NC}"
12868
echo ""
129-
echo -e "${BLUE}📋 What was cleaned up:${NC}"
130-
echo "• Kubernetes deployments, services, and namespaces"
131-
echo "• ALB Controller and Gateway resources"
132-
echo "• ALL Azure resources via resource group deletion:"
133-
echo " - AKS cluster and all associated resources"
69+
echo -e "${BLUE}📋 What's being deleted:${NC}"
70+
echo "• Main resource group: $RESOURCE_GROUP"
71+
echo " - AKS cluster and all nodes"
13472
echo " - Virtual Network and subnets"
13573
echo " - Log Analytics workspace"
13674
echo " - Azure Container Registry"
137-
echo " - Load balancers, public IPs, and networking"
138-
echo "• Resource groups (deletion in progress)"
139-
if [[ "$clean_tf_state" =~ ^[Yy]$ ]]; then
140-
echo "• Local Terraform state files and directories"
141-
fi
75+
echo " - Load balancers and public IPs"
76+
echo " - All networking components"
77+
echo "• AKS node resource group: ${PROJECT_NAME}-node-rg"
78+
echo " - AKS node VMs and disks"
79+
echo " - Load balancers and networking"
14280
if [[ "$delete_state" =~ ^[Yy]$ ]]; then
143-
echo "• Terraform state storage (deletion in progress)"
144-
fi
145-
if [[ "$clean_docker" =~ ^[Yy]$ ]]; then
146-
echo "• Local Docker images"
81+
echo "• Terraform state storage: $TERRAFORM_RG"
14782
fi
14883

14984
echo ""
15085
echo -e "${BLUE}📋 Notes:${NC}"
15186
echo "• Resource group deletions are running in the background"
15287
echo "• It may take 10-15 minutes for all resources to be fully removed"
153-
echo "Deleting resource groups is faster than individual Terraform destroys"
88+
echo "Check deletion progress: az group list --query \"[?contains(name,'$PROJECT_NAME')]\""
15489
echo "• Check Azure Portal to confirm all resources are deleted"
155-
echo "• Azure AD groups and service principals may need manual cleanup"
156-
echo "• Terraform state files preserved locally unless you chose to clean them"
15790

15891
echo ""
15992
echo -e "${GREEN}🎉 DevOps The Hard Way - Azure cleanup completed!${NC}"

0 commit comments

Comments
 (0)