How to delete more than one pod at a time?

11/9/2021

How can I delete more than couple of pods at a time?

The commands I run:

kubectl delete pod pod1
kubectl delete pod pod2
kubectl delete pod pod3

The approach I want to use:

kubectl delete pod pod1 pod2 pod3

Any commands or style that can help me do this? Thanks!

-- cosmos-1905-14
kubernetes

5 Answers

11/10/2021

It's common to front pod with service, you can delete the duo in one line as well:

kubectl delete pod,svc <podname> <servicename>

-- gohm&#39;c
Source: StackOverflow

11/10/2021

In case you need to delete ALL pods in a single namespace, you can use next command:

kubectl delete --all pods --namespace=your_namespace_name
-- Andrew Skorkin
Source: StackOverflow

11/9/2021

The approach that you say that you want:

kubectl delete pod pod1 pod2 pod3

actually works. Go ahead and use it if you want.

In Kubernetes it is more common to operate on subsets that share common labels, e.g:

kubectl delete pod -l app=myapp

and this command will delete all pods with the label app: myapp.

-- Jonas
Source: StackOverflow

11/9/2021

You can use labels.

First label every pod with a common label:

apiVersion: v1
kind: Pod
metadata:
  name: myapp
  labels:
    app: myapp
    env: foo

Then delete them selecting the label:

kubectl delete pod -l env=foo

Documentation

-- Marco Caberletti
Source: StackOverflow

11/9/2021

I am assuming that the pods you want to delete do not share any characteristics. If they do not share any characteristics then you can simply use xargs to pipe the names of the pods to the kubectl delete command like below.

echo "pod1 pod2 pod3" | xargs -n1 | xargs kubectl delete pod   
-- chain_of_dogs
Source: StackOverflow