234

kubectl config view shows contexts and clusters corresponding to clusters that I have deleted.

How can I remove those entries?

The command

kubectl config unset clusters

appears to delete all clusters. Is there a way to selectively delete cluster entries? What about contexts?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Jeremy Lewi
  • 6,386
  • 6
  • 22
  • 37

4 Answers4

313

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.

jeffml
  • 3,471
  • 1
  • 11
  • 4
  • 23
    `kubectl config unset contexts` worked **real nice** thanks! – Randy L Apr 07 '18 at 19:56
  • 1
    I see there is `kubectl config get-contexts` and `kubectl config get-clusters`, but I don't see `kubectl config get-users`, how to list these? – Muhamed Huseinbašić Aug 15 '20 at 09:50
  • 1
    Another SO user reached out to me via e-mail with the following command for listing users: `kubectl config view -o jsonpath='{.users[*].name}'` (their source was [kubectl cheatsheet](https://kubernetes.io/docs/reference/kubectl/cheatsheet/#kubectl-context-and-configuration)) – Muhamed Huseinbašić Oct 11 '20 at 08:53
199

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
Everett Toews
  • 10,337
  • 10
  • 44
  • 45
86

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
Khoa
  • 1,738
  • 1
  • 14
  • 21
31

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


Change context:

kubectx dev-cluster-01

Change namespace:

kubens dev-ns-01

Delete context:

kubectx -d dev-cluster-01

Interested in more power tools and plugins for kubectl? Have a look at Krew, the kubectl plugin manager. Cannot recommend this enough.

hpl002
  • 530
  • 4
  • 7
  • 1
    So if you also want to delete all contexts you might need to deactivate current one with `kubectx -u` and then `kubectx -d current-context-name` – Most Wanted Nov 04 '22 at 17:51
  • 1
    Yes! I love kubectx. Even if this is a 2 year old post, still need to show the love. – beervenger Mar 23 '23 at 13:27