6666fi
6767echo " "
6868
69- print_step " 2" " Destroying Terraform Infrastructure"
70-
71- # Destroy AKS cluster first (longest running)
72- if [ -d " ../2-Terraform-AZURE-Services-Creation/4-aks" ]; then
73- echo -e " ${YELLOW} 📋 Destroying AKS cluster...${NC} "
74- cd " ../2-Terraform-AZURE-Services-Creation/4-aks"
75- terraform destroy -auto-approve || echo -e " ${YELLOW} ⚠️ AKS destroy completed with warnings${NC} "
76- cd ../../scripts
77- fi
78-
79- # Destroy Log Analytics
80- if [ -d " ../2-Terraform-AZURE-Services-Creation/3-log-analytics" ]; then
81- echo -e " ${YELLOW} 📋 Destroying Log Analytics...${NC} "
82- cd " ../2-Terraform-AZURE-Services-Creation/3-log-analytics"
83- terraform destroy -auto-approve || echo -e " ${YELLOW} ⚠️ Log Analytics destroy completed with warnings${NC} "
84- cd ../../scripts
85- fi
86-
87- # Destroy VNET
88- if [ -d " ../2-Terraform-AZURE-Services-Creation/2-vnet" ]; then
89- echo -e " ${YELLOW} 📋 Destroying Virtual Network...${NC} "
90- cd " ../2-Terraform-AZURE-Services-Creation/2-vnet"
91- terraform destroy -auto-approve || echo -e " ${YELLOW} ⚠️ VNET destroy completed with warnings${NC} "
92- cd ../../scripts
93- fi
94-
95- # Destroy ACR
96- if [ -d " ../2-Terraform-AZURE-Services-Creation/1-acr" ]; then
97- echo -e " ${YELLOW} 📋 Destroying Container Registry...${NC} "
98- cd " ../2-Terraform-AZURE-Services-Creation/1-acr"
99- terraform destroy -auto-approve || echo -e " ${YELLOW} ⚠️ ACR destroy completed with warnings${NC} "
100- cd ../../scripts
101- fi
102-
103- echo -e " ${GREEN} ✅ Terraform infrastructure destroyed${NC} "
104- echo " "
105-
106- print_step " 3" " Cleaning up Resource Groups"
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} "
10771echo -e " ${YELLOW} 📋 Deleting main resource group...${NC} "
10872az group delete --name " $RESOURCE_GROUP " --yes --no-wait 2> /dev/null || echo -e " ${YELLOW} ⚠️ Resource group may not exist${NC} "
10973
@@ -112,7 +76,24 @@ az group delete --name "${PROJECT_NAME}-node-rg" --yes --no-wait 2>/dev/null ||
11276
11377echo " "
11478
115- print_step " 4" " Cleaning up Terraform State (Optional)"
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)"
11697echo -e " ${YELLOW} ⚠️ Do you want to delete the Terraform state storage as well?${NC} "
11798echo -e " ${YELLOW} This will remove: ${TERRAFORM_RG} resource group and ${PROJECT_NAME} tfstate storage account${NC} "
11899read -p " Delete Terraform state storage? (y/N): " delete_state
@@ -148,11 +129,16 @@ echo ""
148129echo -e " ${BLUE} 📋 What was cleaned up:${NC} "
149130echo " • Kubernetes deployments, services, and namespaces"
150131echo " • ALB Controller and Gateway resources"
151- echo " • AKS cluster and all associated resources"
152- echo " • Virtual Network and subnets"
153- echo " • Log Analytics workspace"
154- echo " • Azure Container Registry"
132+ echo " • ALL Azure resources via resource group deletion:"
133+ echo " - AKS cluster and all associated resources"
134+ echo " - Virtual Network and subnets"
135+ echo " - Log Analytics workspace"
136+ echo " - Azure Container Registry"
137+ echo " - Load balancers, public IPs, and networking"
155138echo " • Resource groups (deletion in progress)"
139+ if [[ " $clean_tf_state " =~ ^[Yy]$ ]]; then
140+ echo " • Local Terraform state files and directories"
141+ fi
156142if [[ " $delete_state " =~ ^[Yy]$ ]]; then
157143 echo " • Terraform state storage (deletion in progress)"
158144fi
@@ -164,8 +150,10 @@ echo ""
164150echo -e " ${BLUE} 📋 Notes:${NC} "
165151echo " • Resource group deletions are running in the background"
166152echo " • It may take 10-15 minutes for all resources to be fully removed"
153+ echo " • Deleting resource groups is faster than individual Terraform destroys"
167154echo " • Check Azure Portal to confirm all resources are deleted"
168155echo " • Azure AD groups and service principals may need manual cleanup"
156+ echo " • Terraform state files preserved locally unless you chose to clean them"
169157
170158echo " "
171159echo -e " ${GREEN} 🎉 DevOps The Hard Way - Azure cleanup completed!${NC} "
0 commit comments