Kubernetes How to delete PODs based on age/creation time
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Kubernetes does not have a built-in command to delete pods by age. To delete pods older than a certain threshold, you combine kubectl get pods with field selectors, JSONPath output, and shell date arithmetic to filter by creation timestamp. For automated cleanup, Kubernetes provides TTL controllers for finished Jobs and CronJobs for periodic cleanup scripts. This article covers manual deletion commands, automated approaches, and namespace-wide cleanup strategies.
Manual Deletion: Pods Older Than N Hours
This uses jq to:
- Parse the creation timestamp of each pod
- Compare it to the current time
- Select pods where the age exceeds 86400 seconds (24 hours)
- Pipe the pod names to
kubectl delete pod
Using kubectl with awk
Without jq, use kubectl output with awk and date:
Note: The date -d syntax works on Linux (GNU date). On macOS, use date -j -f or install gdate from coreutils.
Deleting by Pod Status and Age
Deleting Evicted Pods
Evicted pods consume etcd storage without running. Clean them up:
TTL Controller for Finished Jobs
Kubernetes has a built-in TTL controller that automatically cleans up finished Jobs:
ttlSecondsAfterFinished automatically deletes the Job and its pods after the specified number of seconds. This is the cleanest solution for Job-based workloads.
CronJob for Automated Cleanup
Create a CronJob that periodically cleans up old pods:
The CronJob needs a ServiceAccount with permission to list and delete pods:
Using kubectl sort-by for Oldest Pods
Common Pitfalls
- Deleting pods managed by a Deployment or ReplicaSet: Deleting a pod that belongs to a Deployment causes the controller to immediately create a replacement. To stop pods permanently, scale the Deployment to zero (
kubectl scale deployment myapp --replicas=0) or delete the Deployment itself. - Using
xargswithout-ron empty input: If thejqfilter matches no pods,xargs kubectl delete podruns with no arguments and may produce an error. Usexargs -r(GNU) to skip execution when input is empty. - Date parsing differences between Linux and macOS: GNU
date -dparses ISO timestamps directly, but macOSdaterequires-j -fwith a format string. Scripts that work on Linux CI may fail on developer macOS machines. - Not scoping cleanup to a namespace: Running cleanup scripts without
-n namespaceoperates on the current context's default namespace. Use--all-namespacesintentionally or always specify-nto avoid accidentally deleting pods in the wrong namespace. - Forgetting RBAC for CronJob-based cleanup: The cleanup CronJob's ServiceAccount needs explicit permissions to list and delete pods. Without a proper Role and RoleBinding, the cleanup pod fails with a 403 Forbidden error.
Summary
- Kubernetes has no built-in "delete pods by age" command — use
kubectl get pods -o jsonwithjqor shell date arithmetic - Use
ttlSecondsAfterFinishedon Jobs for automatic cleanup of finished Job pods - Create a CronJob with kubectl for periodic automated cleanup of old pods
- Delete evicted and completed pods separately using
--field-selector=status.phase - Always consider whether pods are managed by a controller — deleting managed pods triggers immediate recreation
- Use RBAC (ServiceAccount, Role, RoleBinding) for CronJob-based cleanup

