Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete all pods of specific deployments of specific namespaces?

I am looking for a way to delete all pods of specific deployments of specific namespaces. In other words, given:

x - list of namespaces
y - list of deployments

for i in x:
  for j in y:
    delete all pods of deployment j in namespace i

I would be happy if someone knows how to do it in bash commands.

Thanks!

like image 732
yoka791 Avatar asked Oct 31 '25 09:10

yoka791


1 Answers

I believe something like this will do it:

#!/bin/bash

NAMESPACES=( n1 n2 n3 )
DEPLOYMENTS=( dep1 dep2 dep3 )

for i in "${NAMESPACES[@]}"
do
  for x in "${DEPLOYMENTS[@]}"
  do
     # Get the pods in the deployment
     PODS=$(kubectl -n $i get pods --no-headers | awk '{print $1}' | grep $x | tr '\n' ' ')
     kubectl -n $i delete pods $PODS
  done
done
like image 149
Rico Avatar answered Nov 01 '25 23:11

Rico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!