How to Delete a Pod With `kubectl` | Warp

Introducing Warp Factories: open, flexible infrastructure for building your own cloud software factory. Learn More

The short answer

To delete a pod in Kubernetes you can use the kubectl delete pod command as follows:

Bash

$ kubectl delete pod <name>

Where:

For example:

Bash

$ kubectl delete pod nginx-76d6c9b8c-m2hwx

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:

Bash

$ kubectl delete pod <name> --grace-period=<seconds>

Where:

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:

Bash

$ kubectl delete pod <name> --grace-period=0 --force

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:

Bash

$ kubectl delete pod web-0 --wait=false

To wait for a specific amount of time, you can use the --timeout flag as follows:

Bash

$ kubectl delete pod web-0 --timeout=5s

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:

Bash

$ kubectl delete pod -l app=nginx

You can also specify multiple labels by separating them with a comma as follows:

Bash

$ kubectl delete pod -l app=prometheus,component=exporter

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:

Bash

$ kubectl delete pod -l app=nginx --dry-run=server
pod "nginx-76d6c9b8c-sj76d" deleted (server dry run)
pod "web-0" deleted (server dry run)

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:

Bash

$ kubectl delete pod --field-selector=status.phase==Running

The current list of available field selectors is as follows:

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:

Bash

$ kubectl delete pod --field-selector status.phase==Unknown,spec.nodeName=node1

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:

Bash

$ kubectl delete pod --all

To delete all pods in another namespace, you can add the -n flag (short for --namespace) followed by the name of the namespace:

Bash

$ kubectl delete pod --all -n staging

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:

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:

Bash

$ kubectl get pods -o custom-columns="\
NAME:.metadata.name,\
CONTROLLED BY:.metadata.ownerReferences[*].name,\
TYPE:.metadata.ownerReferences[*].kind"

Which will return all the pods in the current namespace with 3 columns:

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.