Fast kubectl mass delete

kubectl delete -A --all

Is very slow, because under the hood it runs 1 delete at a time. (easy to see with -v 8)

A much faster way is to talk to the api directly:

kubectl delete --raw /api/v1/namespaces/foo/pods

That also works with label selectors, for example foo=bar would be:

/api/v1/namespaces/foo/pods?labelSelector=foo%3Dbar

… and deleting everything in foo namespace would be:

/api/v1/pods/namespaces/foo

It does not work without a namespace,
so to delete everything with a given label we have to fetch first and then iterate all namespaces.

Speeding up kubectl with –raw

time kubectl get pods >/dev/null # 12s
time kubect get --raw /api/v1/pods?resourceVersion=0  >/dev/null # 2s

Just using –raw is already faster, but telling the api that you want to read from cache with resourceVersion=0 speeds it up even more and takes load of the api-server. (be careful that reading from cache and using limit= don’t mix)

(Using –raw is similar to using curl directly, but it has the advantage of looking up the api server for you)