Deleting Pending Pods in Kubernetes: A Guide

Feedback


Solution:

Estimating the time a Pod has spent in a specific Status only with the help of

kubectl

can be challenging. Nonetheless, I have a solution that you might find beneficial.

The Pods that are in the

Pending

state and have been in existence for more than X days can be displayed. As an illustration, the following command will show all

Pending

Pods that are more than 5 days old:

kubectl get pods --field-selector=status.phase=Pending --sort-by=.metadata.creationTimestamp | awk 'match($5,/[6-9]d|[0-9][0-9]d|[0-9][0-9][0-9]d/) {print $0}'

The following command can be utilized to remove those pods.

kubectl delete pod $(kubectl get pods --field-selector=status.phase=Pending --sort-by=.metadata.creationTimestamp | awk 'match($5,/[6-9]d|[0-9][0-9]d|[0-9][0-9][0-9]d/) {print $0}')

The

awk

scripting can be modified to align with your specific use case, thereby enabling you to adjust its value as necessary.

Frequently Asked Questions