Delete Kubernetes Pods with a ‘regex’

kubectl does not support delete the resources by a regex pattern. You can delete by label by issuing kubectl delete -l mylabel=value

In general I use awk and xargs together to delete the resources.

Delete all pods and services matches patttern1 and pattern2

$ kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}'| xargs  kubectl delete -n mynamespace pod
$ kubectl get services  -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs  kubectl delete -n mynamespace service

Ismail YENIGUL

Devops Engineer