Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete multiple Kubernetes pods having the same name expression except the latest one?

I have multiple pods running as below. I want to delete them all except the one having minimum age. How to do it?

enter image description here

like image 390
Vatan Soni Avatar asked Feb 02 '26 10:02

Vatan Soni


1 Answers

Something like this? Perhaps also add -l app=value to filter for a specific app

kubectl get pods --sort-by=.metadata.creationTimestamp -o name | head -n -1 | xargs echo kubectl delete

(Remove echo to do it for realz)

like image 199
Janos Lenart Avatar answered Feb 05 '26 03:02

Janos Lenart