Force Delete A Kubernetes Pod
In certain cases, like a node failure, a node delete might not remove the node and it might be needed to “force” delete might be necessary and here’s how
In most cases, deleting a pod is as easy is issuing the following command:
kubectl delete pod $POD_NAME
where $POD_NAME
is the name of the pod
Well sometimes, a pod simply refuses to go and can be force deleted using the following command:
kubectl delete pod $POD_NAME --grace-period=0 --force
And this will delete the pod forever, come what may
Amd just in case there are a tonn of pods to delete and you are using the fish shell, use the following:
for pod in (kubectl get pods --field-selector=status.phase=Failed | awk '{print $1}')
kubectl delete pod $pod --grace-period=0 --force
end
A bash one liner, which deletes evicted pods:
kubectl get pods --all-namespaces -o json | jq '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete pods \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c