The "InUseRouteTableCannotBeDeleted" error occurs when Terraform tries to delete an Azure route table that is still associated with a subnet. Azure prevents deletion of route tables that are actively in use, requiring proper disassociation before removal.
This error means the Azure route table you are trying to delete is still associated with one or more subnets in your virtual network. Azure enforces this restriction as a safety measure to prevent accidental deletion of network infrastructure that is actively routing traffic. The route table must be fully disassociated from all subnets before it can be deleted.
1. Navigate to the Azure Portal
2. Search for "Route tables" and select the problematic route table
3. In the left menu, click "Subnets"
4. Note all subnets currently associated with this route table
5. If no subnets are shown, the association may be managed differently - check your resource group for other references
Instead of using inline route_table_id in azurerm_subnet:
# Bad approach (deprecated)
resource "azurerm_subnet" "example" {
name = "example-subnet"
virtual_network_name = azurerm_virtual_network.example.name
resource_group_name = azurerm_resource_group.example.name
address_prefixes = ["10.0.1.0/24"]
route_table_id = azurerm_route_table.example.id # Problematic
}
# Good approach (recommended)
resource "azurerm_subnet_route_table_association" "example" {
subnet_id = azurerm_subnet.example.id
route_table_id = azurerm_route_table.example.id
}This makes Terraform aware of the dependency and manages the association separately, allowing proper cleanup order during destroy.
If you cannot change the resource structure, add explicit dependencies to your Terraform configuration:
resource "azurerm_route_table" "example" {
name = "example-rt"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example" {
name = "example-subnet"
virtual_network_name = azurerm_virtual_network.example.name
resource_group_name = azurerm_resource_group.example.name
address_prefixes = ["10.0.1.0/24"]
route_table_id = azurerm_route_table.example.id
depends_on = [azurerm_route_table.example]
}This tells Terraform to destroy the subnet before the route table.
If immediate destruction is required, disassociate via Azure Portal:
1. Navigate to the virtual network containing the subnet
2. Go to Subnets in the left menu
3. Select the subnet that uses the problematic route table
4. In the Route table dropdown, select None
5. Click Save
Or via Azure CLI:
az network vnet subnet update \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name mySubnet \
--route-table nullOr via PowerShell:
$subnet = Get-AzVirtualNetworkSubnetConfig -Name mySubnet -VirtualNetwork (Get-AzVirtualNetwork -Name myVNet -ResourceGroupName myResourceGroup)
Set-AzVirtualNetworkSubnetConfig -Name $subnet.Name -VirtualNetwork (Get-AzVirtualNetwork -Name myVNet -ResourceGroupName myResourceGroup) -AddressPrefix $subnet.AddressPrefix -RouteTable $nullAfter manually disassociating the route table or updating your Terraform configuration:
terraform plan -destroy
terraform destroyThe route table should now be deletable. If you still encounter the error, verify that:
- All subnets have been fully disassociated
- No dependent resources (like Azure SQL Managed Instance) are still using the route table
- The route table is not referenced in any Local Network Gateway prefixes
In some cases with Azure SQL Managed Instance, the associated virtual cluster and subnet cannot be deleted for 12 hours and may require Azure support intervention. If you have routes propagating from a Local Network Gateway, remove the relevant address prefixes from the LNG configuration to stop route propagation. When working with AKS clusters, the route table is automatically managed by Azure and disassociation should be handled automatically during cluster deletion. If you are migrating from the deprecated inline route_table_id approach, ensure all your Terraform state is updated to use azurerm_subnet_route_table_association before running destroy on existing infrastructure.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform