Skip to main content
Kubernetes

How to Delete Pods in Kubernetes [Quick K8s Tips]

Deleting a Kubernetes pod is very simple with the kubectl delete pod command but there are specific steps to minimize disruption for your application.

Rakesh Jain

While working on Kubernetes cluster environment, there will be times when you run into a situation where you need to delete pods from one of your worker nodes.

You may need to debug issues with the node itself, upgrade the node, or simply scale down your cluster.

The action of deleting a Kubernetes pod is very simple with the kubectl delete pod command:

kubectl delete pod pod-name

However, there are specific steps you should take to minimize disruption for your application. I'll explain it in detail in this article.

Delete Kubernetes pods gracefully

First, list out all the pods:

root@kmaster-rj:~# kubectl get pods -o wide
NAME                      READY   STATUS    RESTARTS   AGE     IP               NODE          NOMINATED NODE   READINESS GATES
my-dep-557548758d-d2pmd   1/1     Running   0          2d23h   172.16.213.194   kworker-rj2   <none>           <none>
my-dep-557548758d-gprnr   1/1     Running   0          2d23h   172.16.213.49    kworker-rj1   <none>           <none>
pod-delete-demo           1/1     Running   0          4s      172.16.213.196   kworker-rj2   <none>           <none>

Now suppose you want to delete the Pod named "pod-delete-demo", run the following command:

root@kmaster-rj:~# kubectl delete pod pod-delete-demo
pod "pod-delete-demo" deleted

Confirm that the Pod in question is deleted by listing all the pods:

root@kmaster-rj:~# kubectl get pods -o wide
NAME                      READY   STATUS    RESTARTS   AGE     IP               NODE          NOMINATED NODE   READINESS GATES
my-dep-557548758d-d2pmd   1/1     Running   0          2d23h   172.16.213.194   kworker-rj2   <none>           <none>
my-dep-557548758d-gprnr   1/1     Running   0          2d23h   172.16.213.49    kworker-rj1   <none>           <none>

You can see there is no pod-delete-demo pod that is running.

Linux Foundation Kubernetes Certification Discount

Force delete Kubernetes pods

Why do you need force pod deletion??

Sometimes the Pod gets stuck in terminating/unknown state on an unreachable Node after a timeout.

Pods may also enter these states when the user attempts graceful deletion of a Pod on an unreachable Node.

In those scenarios, you can delete the Pod forcefully.

If you want to delete a Pod forcibly using kubectl version >= 1.5, do the following:

kubectl delete pods pod_name --grace-period=0 --force

If you're using any version of kubectl <= 1.4, you should omit the --force option and use:

kubectl delete pods pod_name --grace-period=0

Now let's delete the pod "pod-delete-demo" using the above method:

root@kmaster-rj:~# kubectl delete pod pod-delete-demo --force --grace-period=0 --namespace=default
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "pod-delete-demo" force delete

NOTE: Force pod deletions do not wait for confirmation from the kubelet that the Pod has been terminated.

If even after these commands the pod is stuck in Unknown state, use the following command to remove the pod from the cluster:

kubectl patch pod pod-delete-demo -p '{"metadata":{"finalizers":null}}'

Confirm that the pod has been deleted:

root@kmaster-rj:~# kubectl get pods -o wide
NAME                      READY   STATUS    RESTARTS   AGE     IP               NODE          NOMINATED NODE   READINESS GATES
my-dep-557548758d-d2pmd   1/1     Running   0          2d23h   172.16.213.194   kworker-rj2   <none>           <none>
my-dep-557548758d-gprnr   1/1     Running   0          2d23h   172.16.213.49    kworker-rj1   <none>           <none>

Hope you find this Kubernetes tip helpful. Stay subscribed for more DevOps tips and tutorials .