This error occurs when Terraform or kubectl attempts to create a Kubernetes Deployment while the target namespace is in a Terminating state. The namespace is stuck in the process of being deleted, preventing new resources from being created.
Kubernetes namespaces use finalizers as cleanup checks before deletion. When you delete a namespace, it enters a Terminating state while Kubernetes removes dependent resources and clears finalizers. If finalizers cannot be cleared or resources cannot be deleted, the namespace remains stuck in Terminating state indefinitely. Any attempt to create new resources in this namespace will be rejected.
Verify the namespace status with:
kubectl get namespace <namespace-name>If the STATUS column shows "Terminating", proceed to the next step.
List all resources in the namespace that might have finalizers:
kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n <namespace-name>Also check the namespace finalizers directly:
kubectl get namespace <namespace-name> -o json | jq ".spec.finalizers"For each resource that cannot be deleted normally, force delete it:
kubectl delete <resource-type> <resource-name> --namespace <namespace-name> --grace-period=0 --force --wait=falseExample for a stuck ConfigMap:
kubectl delete configmap problematic-config -n <namespace-name> --grace-period=0 --force --wait=falseIf the namespace is still stuck after removing resources, remove the finalizers blocking deletion:
kubectl patch namespace <namespace-name> --patch '{"metadata": {"finalizers": null}}'Alternatively, use this one-liner:
kubectl get namespace <namespace-name> -o json | jq 'del(.spec.finalizers)' | kubectl replace --raw "/api/v1/namespaces/<namespace-name>/finalize" -f -Confirm the namespace has been deleted:
kubectl get namespace <namespace-name>You should receive an error: Error from server (NotFound): namespaces "<namespace-name>" not found
Once the namespace is fully deleted, you can recreate it and your Terraform-managed resources:
terraform applyEnsure your Terraform configuration includes the namespace resource with proper lifecycle management.
Finalizers are Kubernetes cleanup guards. While removing them resolves the immediate issue, investigate why they could not be cleared. Common causes include: unhealthy webhooks preventing object deletion, stuck API servers, or resources with external dependencies (like storage or networking). In Terraform, prevent namespace termination issues by: (1) using kubernetes_namespace resource with proper dependencies, (2) ensuring all dependent resources are properly managed in Terraform, (3) using explicit depends_on statements for resource ordering. For GKE, note that resource deletion may have delays; check the Kubernetes version compatibility. For production environments, consider using namespace quotas and resource policies to prevent accidental deletions.
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