Resi Dwi Thawasa

TIL

Delete all evicted pods in one line

Evicted pods pile up and clutter kubectl get pods. They are done, they just sit there. Here is a one-liner to clean them up across all namespaces:

kubectl get pods --all-namespaces -o json \
  | jq '.items[] | select(.status.reason=="Evicted") | "kubectl delete pod \(.metadata.name) -n \(.metadata.namespace)"' \
  | xargs -n 1 bash -c

It pulls every pod as JSON, filters to the ones with reason Evicted, builds a delete command for each, and runs them one at a time with xargs. The \(...) bits are jq string interpolation.

Worth a dry run first: drop the xargs part and just look at the commands it would run.

#kubernetes #kubectl