Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes: How do I delete clusters and contexts from kubectl config?

People also ask

How do I remove a cluster from kube config?

To remove a cluster, you can run kubectl --kubeconfig=config-demo config unset clusters. <name> To remove a context, you can run kubectl --kubeconfig=config-demo config unset contexts.

What is kubectl config context?

Context. A context element in a kubeconfig file is used to group access parameters under a convenient name. Each context has three parameters: cluster, namespace, and user. By default, the kubectl command-line tool uses parameters from the current context to communicate with the cluster.

What command do you use to delete everything on your cluster?

Instead, the resource can be targetted directly using the kubectl delete command. This method is more effective when targeting a group of resources or for deleting all resources in the cluster or a namespace.


kubectl config unset takes a dot-delimited path. You can delete cluster/context/user entries by name. E.g.

kubectl config unset users.gke_project_zone_name

kubectl config unset contexts.aws_cluster1-kubernetes

kubectl config unset clusters.foobar-baz

Side note, if you teardown your cluster using cluster/kube-down.sh (or gcloud if you use Container Engine), it will delete the associated kubeconfig entries. There is also a planned kubectl config rework for a future release to make the commands more intuitive/usable/consistent.


For clusters and contexts you can also do

kubectl config delete-cluster my-cluster

kubectl config delete-context my-cluster-context

There's nothing specific for users though, so you still have to do

kubectl config unset users.my-cluster-admin

Run command below to get all contexts you have:

$ kubectl config get-contexts

CURRENT   NAME             CLUSTER     AUTHINFO                                NAMESPACE

*         Cluster_Name_1   Cluster_1   clusterUser_resource-group_Cluster_1

Delete context:

 $ kubectl config delete-context Cluster_Name_1

Unrelated to question, but maybe a useful resource.

Have a look at kubectx + kubens: Power tools for kubectl.

They make it easy to switch contexts and namespace + have the option to delete

e.g.,

kubectx -d my-context