How to delete multiple namespaces with label option

12/15/2019

I have generated a bunch of namespaces like below and now i want to delete only these name spaces without deleting kube-system namespaces,i tried with grep but no success

kubectl delete namespaces | grep "gatling*" error: resource(s) were provided, but no name, label selector, or --all flag specified

Multiple namespaces

-- mark liberhman
kubectl
kubernetes

1 Answer

12/15/2019

First get the names of the namespaces you want to delete:

kubectl get namespaces --no-headers=true -o custom-columns=:metadata.name | grep gatling

With -o custom-columns=:metadata.name we output only the names of the services. The output is piped to grep, which filters them by looking for gatling.

Then run the delete command for each line with xargs:

kubectl get namespaces --no-headers=true -o custom-columns=:metadata.name | grep gatling | xargs kubectl delete namespace
-- Dávid Molnár
Source: StackOverflow