Delete multiple CRDs with PowerShell and kubectl

Context #

I am testing different load balancer implementations in our Kubernetes clusters and all of them install multiple Custom Resources Definition (CRD). Unfortunately, the CRDs are not automatically removed when the Helm Chart for the corresponding load balancer is deleted.

Since each load balancer adds several CRDs objects, I didn't want to manually delete each individual CRD resource using the kubectl delete crd <crd_name> command.

Implementation #

To streamline the process, I leverage PowerShell to filter and manipulate the output of the k get crds commands, as shown below:

To delete the CRDs for a single load balancer:

kubectl get crds |
Select-String kong |
ForEach-Object { ($_ -split "\s")[0] } |
ForEach-Object { kubectl delete crds $_ }

To delete the CRDs for multiple load balancers at once:

kubectl get crds |
Select-String "istio", "ambassador" |
ForEach-Object { ($_ -split "\s")[0] } |
ForEach-Object { kubectl delete crds $_ }

Note #

Before running the full command, I would suggest to remove the ForEach-Object { kubectl delete crds $_ } line and verify that the output is as expected.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.