<p>We have one cluster where it seems that namespaces never want to be deleted completely and now can't re-create custom-metrics namespace to be able to collect custom metrics to properly setup HPA. I fully understand that I can create another namespace with all custom-metrics resources, but a little concerned with the overall health of the cluster, given that the namespaces get stuck in "Terminating" state</p>

<pre><code>$ kubectl get ns
NAME             STATUS        AGE
cert-manager     Active        14d
custom-metrics   Terminating   7d
default          Active        222d
nfs-share        Active        15d
ingress-nginx    Active        103d
kube-public      Active        222d
kube-system      Active        222d
lb               Terminating   4d
monitoring       Terminating   6d
production       Active        221d
</code></pre>

<p>I already tried to export namespaces to JSON, delete finalizers and re-create using edited JSON files. also tried to kubectl edit ns custom-metrics and delete "- kubernetes" finalizer. all to no avail.</p>

<p>does anyone have any other recommendations on how else I can try to destroy these "stuck" namespaces" </p>

<p>curl to <a href="https://master-ip/api/v1/namespace/...../finalize" rel="noreferrer">https://master-ip/api/v1/namespace/...../finalize</a> doesn't seem to work on Google Kubernetes Engine for me, I'm assuming these operations are not allowed on GKE cluster</p>

<p>Trying things like doesn't work as well:</p>

<pre><code>$ kubectl delete ns custom-metrics --grace-period=0 --force
</code></pre>

<blockquote>
  <p>warning: Immediate deletion does not wait for confirmation that the
  running resource has been terminated. The resource may continue to run
  on the cluster indefinitely. Error from server (Conflict): Operation
  cannot be fulfilled on namespaces "custom-metrics": The system is
  ensuring all content is removed from this namespace.  Upon completion,
  this namespace will automatically be purged by the system.</p>
</blockquote>

<p>and there're no resources listed in this namespaces at all:
<code>kubectl get all -n custom-metrics</code> or looping through all api-resources in this namespace shows no resources exist at all:
<code>kubectl api-resources  --namespaced=true -o name | xargs -n 1 kubectl get -n custom-metrics</code></p>

<p>I did something similar to rahul.tripathi except the curl did not work for me - I followed <a href="https://medium.com/@craignewtondev/how-to-fix-kubernetes-namespace-deleting-stuck-in-terminating-state-5ed75792647e" rel="noreferrer">https://medium.com/@craignewtondev/how-to-fix-kubernetes-namespace-deleting-stuck-in-terminating-state-5ed75792647e</a> which does the following:</p>
<pre><code>NAMESPACE=
kubectl get namespace $NAMESPACE -o json &gt; $NAMESPACE.json
sed -i -e 's/&quot;kubernetes&quot;//' $NAMESPACE.json
kubectl replace --raw &quot;/api/v1/namespaces/$NAMESPACE/finalize&quot; -f ./$NAMESPACE.json
</code></pre>
<p>Voila! Namespace is deleted</p>
<p>Update:  One-liner version of this solution (requires jq)</p>
<pre><code>NAMESPACE= ; kubectl get namespace $NAMESPACE -o json | jq 'del(.spec.finalizers[0])' | kubectl replace --raw &quot;/api/v1/namespaces/$NAMESPACE/finalize&quot; -f -
</code></pre>
<p>Update #2: Terraform version</p>
<pre><code>resource &quot;kubernetes_namespace&quot; &quot;this&quot; {
  for_each = toset( var.namespaces )
  metadata {
    name = each.key
  }
  provisioner &quot;local-exec&quot; {
    when = destroy
    command    = &quot;nohup ${path.module}/namespace-finalizer.sh ${each.key} 2&gt;&amp;1 &amp;&quot;
  }
}
</code></pre>
<p>namespace-finalizer.sh</p>
<pre><code>sleep 30; kubectl get namespace $1 &amp;&amp; kubectl get namespace $1 -o json | jq 'del(.spec.finalizers[0])' | kubectl replace --raw &quot;/api/v1/namespaces/$1/finalize&quot; -f -
</code></pre>
