Delete all resource in kubernetes with helm go sdk

5/1/2021

I am using widely the helm go sdk. Now i do not know always which resources are still in the cluster and would have a call to completely clean it.

I can get the KubeClient, but found only the delete method expecting the resources.

Maybe the programmatical call to kubectl delete all --all? Best would be including the namespace resource itself.

Or do I need to request the names of all resources and then delete it?

Thank you!

-- Langohr
go
kubectl
kubernetes
kubernetes-helm

1 Answer

5/11/2021

This is a community wiki answer posted for better visibility. Feel free to expand it.

As already mentioned in the comments, the way to list all the resources would be by using a proper kubectl command. The idea behind it is well explained here:

kubectl api-resources enumerates the resource types available in your cluster.

this means you can combine it with kubectl get to actually list every instance of every resource type in a namespace:

kubectl api-resources --verbs=list --namespaced -o name \
  | xargs -n 1 kubectl get --show-kind --ignore-not-found -l <label>=<value> -n <namespace>

With that you will be able to request names of all the resources and than delete them.

-- Wytrzymały Wiktor
Source: StackOverflow