One Liner script to delete all pods from the K8s
Rahul Bhatia
AWS|DevOps|Linux|GIT|Docker| K8s|Terraform | Helm| ArgoCD | Prompt Engineering
Here how the script worked (For those who are beginners and not aware of bash scripting)
- kubectl get pods | awk '{print $1}' | tail +2 => awk '{print $1}' will print the first column of the "kubectl get pods" command output which is the list of total number of pods. Further tail +2 is used to remove the column header NAME, or in other words , this will extract the output from the second line onwards. piping(|) is used for re-direction i.e transfer of standard output of one command to next command.
- Once we get our desired list of PODs as shown, use | in the previous command and run xargs kubectl delete pod. (kubectl get pods | awk '{print $1'} | tail +2|xargs kubectl delete pod) Here, xargs is a unix command that is used to build and execute command from the standard input.