Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Last active April 13, 2023 06:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zulhfreelancer/a5c2f2cb36146b793c82e5b1167ca90e to your computer and use it in GitHub Desktop.
Save zulhfreelancer/a5c2f2cb36146b793c82e5b1167ca90e to your computer and use it in GitHub Desktop.
How to delete all evicted pods from all namespaces?
#!/bin/bash
# This script is for Mac OSX users. If you are on Linux, replace 'gxargs' with 'xargs'.
# To install 'gxargs' command using Homebrew, run 'brew install findutils' first.
# Without parallel (ideal for small number of pods)
kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | gxargs -n 1 -d '\n' bash -c 'kubectl delete pod $0 $1'
# With parallel (ideal for big number of pods)
# To install 'parallel' command using Homebrew, run 'brew install parallel' first.
# To install 'parallel' command on Linux (e.g. Ubuntu), refer https://askubuntu.com/a/298598/972977 for instructions.
kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | gxargs -n 1 -d '\n' bash -c 'echo kubectl delete pod $0 $1' | parallel
# References:
# - https://superuser.com/a/467284
# - https://gist.github.com/svx/a2310feb53fb71ad232bd7f66fada5bc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment