Deleting a Pod with `kubectl`
Gabriel Manricks
Chief Architect, ClearX
Published: 1/31/2024
The short answer
To delete a pod in Kubernetes you can use the kubectl delete pod command as follows:
$ kubectl delete pod <name>
Run in Warp
Where:
- name is the name of the pod you want to delete.
For example:
$ kubectl delete pod nginx-76d6c9b8c-m2hwx
Run in Warp
By default, this will cause the terminal to hang until the object is fully removed and output a message upon successful deletion.
Easily retrieve this command using Warp’s AI Command Search
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:
Pressing the # symbol at the start of a line will pull up the AI search and then you can enter delete pod to get the command template. You can quickly insert that command into your shell using CMD+ENTER.
Customizing a Pod's shutdown grace period
By default, Kubernetes will wait 30 seconds for the Pod to complete any post-run hooks and for the main process to close after receiving the signal to terminate.
If you want to give the process more (or less) time, you can use the --grace-period flag as follows:
$ kubectl delete pod <name> --grace-period=<seconds>
Run in Warp
Where:
- name is the name of the pod you want to delete.
- seconds is the number of seconds grace to give the pod.
Note that, 1 is the lowest number of seconds you can wait. Setting the value to a negative number will cause the flag to be ignored. Finally, setting the flag to 0 will skip the grace period (and hooks) altogether and can only be used together with the --force flag like in the following example:
$ kubectl delete pod <name> --grace-period=0 --force
Run in Warp
This combination will immediately remove the pod from Kubernetes without waiting for confirmation that the pod is terminated and the containers will be sent a signal to terminate immediately. This option should be used with care, as if there is an error which causes the container to not be deleted, for example, if a core service is not responding, then the containers might continue to run in the background without you seeing them in kubernetes.
Deleting a Pod without waiting
By default, the kubectl command will hang the terminal until the object is fully removed. If the object has a large grace period this might take a while and you may want to do other things in your terminal in the meantime.
To skip the wait period, you can use the --wait=false flag as follows:
$ kubectl delete pod web-0 --wait=false
Run in Warp
To wait for a specific amount of time, you can use the --timeout flag as follows:
$ kubectl delete pod web-0 --timeout=5s
Run in Warp
The above command will wait 5 seconds (notice the s suffix for seconds) and if the pod does not close within that amount of time, the kubectl delete command will be considered as failed and will display a timeout error in your terminal. Note that, this doesn't mean that the deletion wasn’t a success—your pods will continue to terminate in the background—this only means your pods were not entirely deleted in the specified time.
Deleting a Pod by label
There are times when it is easier to delete Pods by label—this can be because the label is constant and the name of the Pod can change, for example, in a Deployment. Deleting by label is easier for automation scripts in cases like these. Another reason could be if you want to delete many different pods sharing the same label whereas each pod would have a unique name.
To delete a pod by label, you can use the --selector flag or -l flag for short as follows:
$ kubectl delete pod -l app=nginx
Run in Warp
You can also specify multiple labels by separating them with a comma as follows:
$ kubectl delete pod -l app=prometheus,component=exporter
Run in Warp
The above command will delete any pods with both label key values:
- 1. app: prometheus
- 2. component: exporter
Doing a dry-run before deleting Pods
When using labels or field selectors to delete pods, you might want to verify which pods will be deleted by your command before actually deleting them.
To perform a dry run, you can use the --dry-run=server kubectl flag as follows:
$ kubectl delete pod -l app=nginx --dry-run=server
pod "nginx-76d6c9b8c-sj76d" deleted (server dry run)
pod "web-0" deleted (server dry run)
Run in Warp
Deleting a Pod by field selector
Pods can be deleted based on certain values using the --field-selector parameter. Similarly to deleting Pods by label, the field selector is a way to delete all pods matching a certain query.
For example, to delete all pods that are currently running, you can run:
$ kubectl delete pod --field-selector=status.phase==Running
Run in Warp
The current list of available field selectors is as follows:
- metadata.name - the name of the Pod
- metadata.namespace - the namespace of the Pod
- spec.nodeName - the name of the Node the Pod is running on
- spec.restartPolicy - the restart policy of the Pod
- spec.schedulerName - the name of the Scheduler in charge of deploying the Pod
- spec.serviceAccountName - the name of the service account the Pod is using
- spec.hostNetwork - filter based on if the Pod is connected to the host’s network
- status.phase - the lifecycle phase (status) of the Pod
- status.podIP - filter based on the Pod’s IP
- status.nominatedNodeName - filter based on the name of the Node that is the current “nominated” candidate to schedule the Pod.
For each of these fields, you can use the == or != comparison operators to filter the Pods that will be deleted.
To chain multiple conditions together you can combine them using a comma like so:
$ kubectl delete pod --field-selector status.phase==Unknown,spec.nodeName=node1
Run in Warp
The above command will delete all pods stuck in the “Unknown” state,. which are also running on a node called node1.
Deleting all Pods at once
To delete all pods in the default namespace, you can use the --all flag as follows:
$ kubectl delete pod --all
Run in Warp
To delete all pods in another namespace, you can add the -n flag (short for --namespace) followed by the name of the namespace:
$ kubectl delete pod --all -n staging
Run in Warp
Note that, it is usually recommended to run a dry run first before deleting all Pods.
Troubleshooting Pods deletion
If you delete a pod and it keeps coming back, chances are it is being controlled by some kind of replication set.
The types of replication sets that exist are:
- Replicaset: used for basic replication and by deployments.
- Statefulset: used for replication where not all replicas are exactly the same.
- Daemonset: used for replication across all nodes, making sure each node in your cluster runs a certain pod.
These sets define the desired number of replicas for a given pod, so if you delete a running pod controlled by one of these, they will make sure a new pod gets scheduled in its place to meet the desired replicas defined in them.
To check what is controlling your pod, you can use the kubectl get pods command:
$ kubectl get pods -o custom-columns="\
NAME:.metadata.name,\
CONTROLLED BY:.metadata.ownerReferences[*].name,\
TYPE:.metadata.ownerReferences[*].kind"
Run in Warp
Which will return all the pods in the current namespace with 3 columns:
- NAME: the name of the pod.
- CONTROLLED BY: the name of the replication set.
- TYPE: the type of the replication set.
In cases like these, you will need to scale down / delete the source resource which will in turn terminate the pod permanently.
As mentioned above, Deployments also use Replicasets so if you see a Replicaset then you might need to scale down the Deployment and not directly the Replicaset. You can view the owner reference of the Replicaset to see if it is standalone or has a further parent element.
Written by
Gabriel Manricks
Chief Architect, ClearX
Filed Under
Related Articles
Copy Files From Pod in Kubernetes
Learn how to copy files and directories from within a Kubernetes Pod into the local filesystem using the kubectl command.
Scale Deployments in Kubernetes
Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.
Get Kubernetes Logs With kubectl
Learn how to get the logs of pods, containers, deployments, and services in Kubernetes using the kubectl command. Troubleshoot a cluster stuck in CrashloopBackoff, ImagePullBackoff, or Pending error states.
Forward Ports In Kubernetes
Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.
Tail Logs In Kubernetes
Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.
Get Context In Kubernetes
Learn how to get information about one or more contexts in Kubernetes using the kubectl command.
Delete Kubernetes Namespaces With kubectl
Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.
Get Kubernetes Secrets With kubectl
Learn how to list, describe, customize, sort and filter secrets in a Kubernetes cluster by name, type, namespace, label and more using the kubectl command.
List Kubernetes Namespaces With kubectl
Learn how to list, describe, customize, sort and filter namespaces in a Kubernetes cluster by name, label, and more using the kubectl command.
How To List Events With kubectl
Learn how to list and filter events in Kubernetes cluster by namespace, pod name and more using the kubectl command.
Kubernetes vs Docker: The Backbone of Modern Backend Technologies
Lean the fundamentals of the Kubernetes and Docker technologies and how they interplay with each other.
Set Context With kubectl
Learn how to create, modify, switch, and delete a context in Kubernetes using the kubectl config command.