Kubernetes: can't delete namespace

11/13/2018

I just created a namespace, have done nothing with it and now deleted it. However, when I list contexts I can still see it there. It seems to have been deleted as I can't delete it again. Why can I still see it listed when I get contexts?

kubectl config get-contexts
CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
*         dev        minikube   minikube   dev
          minikube   minikube   minikube

kubectl delete namespace dev
namespace "dev" deleted

kubectl config get-contexts
CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
*         dev        minikube   minikube   dev
          minikube   minikube   minikube

I switched contexts just in case but still get the same problem. E.g.

kubectl delete namespace dev
Error from server (NotFound): namespaces "dev" not found

kubectl config get-contexts
CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
          dev        minikube   minikube   dev
*         minikube   minikube   minikube

Interestingly, I don't see it when I list namespaces. E.g.

kubectl get namespaces
-- Snowcrash
kubernetes

1 Answer

11/13/2018

A context in kubectlis just a local config that contains details (metadata) about a particular cluster or a namespace. This is the config that is needed for cluster/namespace management using the kubectl client.

So, when you type kubectl config <any_command>, it's just doing a lookup in a file stored locally on you computer. Run the following to know more about this command and how to control the location of the config file:

kubectl config --help

Deleting a cluster or a namespace does not delete the associated context. The reason is that the deleting of a cluster or a namespace is an asynchronous operation that runs on the cluster. This operation may take longer than a few seconds to actually finish. Thus, kubectl cannot immediately delete the context from the config file after you issues the delete to the cluster master.

To answer your question, you have to manually delete the context using:

kubectl config delete-context dev
-- Ashu Pachauri
Source: StackOverflow