Reputation: 851
I have the following pods:
NAME READY STATUS RESTARTS AGE
xxx-myactivities-79f49cdfb4-nwg22 1/1 Terminating 0 10h
xxx-mysearch-55864b5c59-6bnwl 1/1 Terminating 0 1d
xxx-mysearch-55864b5c59-rpn48 1/1 Terminating 0 13h
xxx-mysearch-6ff9bbb7cb-9qgbb 1/1 Terminating 0 3d
I am running the following code to forcefully delete those pods:
#
# Clean up dying pods
#
pods=$( kubectl get pods | grep -v Running | tail -n +2 | awk -F " " '{print $1}' )
for pod in $pods;
do
kubectl delete pod $pod --force
done
Here is the output:
pod "xxx-myactivities-79f49cdfb4-nwg22" deleted
pod "xxx-mysearch-55864b5c59-6bnwl" deleted
pod "xxx-mysearch-55864b5c59-rpn48" deleted
pod "xxx-mysearch-6ff9bbb7cb-9qgbb" deleted
After cleaning up, those pods still hang around.
NAME READY STATUS RESTARTS AGE
xxx-myactivities-79f49cdfb4-nwg22 1/1 Terminating 0 10h
xxx-mysearch-55864b5c59-6bnwl 1/1 Terminating 0 1d
xxx-mysearch-55864b5c59-rpn48 1/1 Terminating 0 13h
xxx-mysearch-6ff9bbb7cb-9qgbb 1/1 Terminating 0 3d
How do I clean up those pods?
Upvotes: 82
Views: 211122
Reputation: 5886
I recently had a pod that couldn't be removed by any of the methods listed so far. It turns out this was because of a finalizer on the pod. The operator that handled this finalizer had already been deleted.
To work around this I did kubectl edit pods/{the pod name}
, then manually removed the finalizer and saved it. After that it immediately disappeared.
Upvotes: 1
Reputation: 6841
Please wait and reconsider the approach. The force-killed pod in most situations will continue running, so its probably better idea to get inside the pod (using kubectl exec
or oc rsh
) and kill any offending processes first (e.g. those that exhaust resources). Once force-killed, the pod will no longer be accessible through the k8s API (its zombie processes that continue running despite pod being no longer there will then have to be killed by someone with the root on the host).
Upvotes: 0
Reputation: 4003
I use this one to forcefully delete all pods that are not in a Running state:
kubectl get po | grep -v Running | awk 'NR>1 {print $1}' | xargs kubectl delete po --force --grace-period=0
Make sure you are in the namespace you want to work with.
If you want to delete all pods in Running state, remove the -v
flag.
Upvotes: 3
Reputation: 1027
For deleting pod forcefully use this.
kubectl delete pod <Pod_Name> -n <namespace_name> --grace-period=0 --force
OR
kubectl delete pod <Pod_Name> -n <namespace_name> --wait=false
For reference please follow below link. https://kubernetes.io/docs/tasks/run-application/force-delete-stateful-set-pod/
Upvotes: 28
Reputation: 57282
I'm using this:
kubectl get pods | grep part-of-the-pods-name|awk '{split($0,a," "); print a[1]}'|xargs -I {} kubectl delete pods {} --grace-period=0
additional grep can be added if needed.
Upvotes: 1
Reputation: 2037
In case you used Helm to install the pod just run
helm delete Your_Deployment_Name -n The_Namespace
Upvotes: 2
Reputation: 10711
To clean the pods you need to delete their deployments namespace.
First discover that deployments existed:
$ kubectl get deployments --all-namespaces
NAME READY STATUS RESTARTS AGE
chetabahana-web-584b95d576-62ccj 1/1 Running 0 20m
tutorial-web-56fbccc56b-wbwjq 1/1 Running 0 1m
Delete the deployment <NAME>-xxxx
like this:
$ kubectl delete deployment <NAME>
For example to delete tutorial-web-56fbccc56b-wbwjq
run:
$ kubectl delete deployment tutorial
Then all corresponded pods of tutorial-xxxx
will terminate by itself.
NAME READY STATUS RESTARTS AGE
chetabahana-web-584b95d576-62ccj 1/1 Running 0 20m
tutorial-web-56fbccc56b-wbwjq 0/1 Terminating 0 1m
Upvotes: 97
Reputation: 1087
kubectl get pod --all-namespaces | awk '{if ($4 != "Running") system ("kubectl -n " $1 " delete pods " $2 " --grace-period=0 " " --force ")}'
you can use this command
Upvotes: 5
Reputation: 929
To delete all pods in terminating state with one command do:
for p in $(kubectl get pods | grep Terminating | awk '{print $1}'); do kubectl delete pod $p --grace-period=0 --force;done
Upvotes: 9
Reputation: 1578
You have these alternatives:
kubectl delete pod xxx --now
Or
SSH into the node the stuck pod was scheduled on
Running docker ps | grep {pod name}
to get the Docker Container ID
Running docker rm -f {container id}
Or
kubectl delete pod NAME --grace-period=0 --force
Upvotes: 104