How to clean up finished AI agent Jobs with a TTL in Kubernetes

Set spec.ttlSecondsAfterFinished on a Job and Kubernetes deletes that Job, with its Pods, a set number of seconds after it reaches Complete or Failed. Agent batch Jobs pile up fast, so this is the built-in cleanup path. Pair it with log and audit retention, because the Pods hold the output you will want later.
What the TTL controller deletes
The TTL-after-finished controller applies only to Jobs. It cleans up finished Jobs, either Complete or Failed, that set .spec.ttlSecondsAfterFinished. Cleanup is cascading: the controller deletes the Job’s dependent objects, such as its Pods, together with the Job, so the Pods and the output they hold go with it.
Kubernetes still honors lifecycle guarantees on the Job, such as waiting for finalizers, so a finalizer can hold the Job in place longer than the TTL alone would. There is no retention exception for logs, events or the audit trail. Treat the Pods as gone when the Job is gone, and plan retention on that basis.
When the countdown starts
The timer starts once the Job’s status condition changes to show Complete or Failed. A Job that is still running has no countdown yet, so the TTL is not a runtime limit and does not stop a running agent. Bound run time with activeDeadlineSeconds instead.
The TTL is also not fixed at creation. You can set it at any time, including on a Job that has already finished. Kubernetes lists five ways to set it: specify the field in the Job manifest; manually set the field on existing, already finished Jobs; use a mutating admission webhook to set it at Job creation time; use a mutating admission webhook to set it after the Job has finished; or write your own controller to manage the cleanup TTL for Jobs that match a selector. A webhook at creation time is the option that lets one policy set a TTL for every agent Job.
Two caveats before you rely on the TTL
Two caveats in the Kubernetes docs matter for agent Jobs. First, if you extend the TTL after its period has expired, Kubernetes “doesn’t guarantee to retain that Job, even if an update to extend the TTL returns a successful API response.” If you need a Job kept for an investigation, change the TTL before it expires, not after.
Second, the TTL uses timestamps stored in the cluster, so the feature is sensitive to time skew. Skew can make the control plane clean up Job objects at the wrong time, which matters most when you set a non-zero TTL. Keep cluster clocks accurate, and leave a margin when you choose a short window.
Set ttlSecondsAfterFinished on an agent batch Job
- Choose a window in seconds. A value of
0makes the Job eligible for deletion immediately after it finishes, so pick a value that outlasts log collection and audit review. - Add the field under
spec, as in the example. The sample image and command are the Kubernetes docs example; swap in your agent’s. - Keep
backoffLimit, and setactiveDeadlineSecondsto bound run time. When the deadline passes, Kubernetes terminates the running Pods and marks the JobFailedwith reasonDeadlineExceeded. - Apply the manifest with
kubectl apply -fand check the Job while it runs. - Export logs and audit records to your own store before the TTL expires.
apiVersion: batch/v1
kind: Job
metadata:
name: pi-with-ttl
spec:
ttlSecondsAfterFinished: 100
backoffLimit: 4
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
kubectl apply -f ./job.yaml
kubectl get job pi-with-ttl -o yaml
kubectl logs jobs/pi-with-ttl
Check that ttlSecondsAfterFinished appears in the kubectl get job output after you apply it. kubectl logs jobs/pi-with-ttl prints the Job’s output, read from its Pod, for as long as the Job exists.
Keep the evidence past the Job
When the TTL controller deletes a Job, it deletes its Pods with it, and the Pods are where kubectl logs reads the output. The Job’s lifetime is therefore the lifetime of its logs in the cluster.
Write the records to a store you control before the TTL runs. Ship container logs to your log platform, and keep the Kubernetes audit records for the agent’s service account in the audit backend. Then the TTL can remove the Job without removing the evidence. The audit post for AI agent actions in Kubernetes covers what an audit event records and how to set one up.
Compare cleanup options
Pick the option that matches how the Job was created.
| Approach | Behaviour | What happens to Pods and output | Use it when |
|---|---|---|---|
| No TTL (field unset) | The TTL controller does not clean up that Job after it finishes | The Job stays in the API until something removes it. Finished Jobs left in the system put pressure on the API server | Rarely, for a Job you clean up yourself on a schedule |
| CronJob cleanup | Jobs managed by a higher-level controller such as CronJobs are cleaned up by CronJobs under their capacity-based cleanup policy | The CronJob controller removes the Jobs it created | Scheduled agent runs that a CronJob creates |
TTL controller (ttlSecondsAfterFinished) |
Deletes the finished Job after the TTL; cascading delete; stable since Kubernetes v1.23 | The Job’s Pods are deleted with it, so their logs go too | Agent Jobs you create directly with kubectl apply |
Manual kubectl delete |
Deleting a Job cleans up the Pods it created | Happens when you run the command, so you choose the moment | One-off cleanup, or a pipeline that already exports logs first |
Where this fits
The TTL is one control among several for agent workloads. Pod-level settings for these Jobs are covered in the Pod Security Admission levels for AI agent workloads post. Unmanaged Jobs, those created directly and not through a workload API such as CronJob, have a default deletion policy of orphanDependents. That can leave Pods behind after the Job is deleted, until the control plane garbage collects them, which is one more reason the Kubernetes docs recommend setting the TTL.
Because kubectl logs jobs/<name> reads from the Job’s Pods, set the cleanup window and the log retention together, not one after the other. For the wider governance context, see the governing AI agents hub.
Sources
- Kubernetes: Automatic Cleanup for Finished Jobs (fetched 2026-11-23): the TTL controller, cascading deletion, when the timer starts, stable since v1.23, extending a TTL, and time skew.
- Kubernetes: Jobs (fetched 2026-11-23): the
ttlSecondsAfterFinishedexample,0and unset behaviour, the orphanDependents note,backoffLimit,activeDeadlineSeconds, and thekubectlcommands for Job logs.
Frequently asked questions
What does ttlSecondsAfterFinished do in a Kubernetes Job?
It tells the TTL-after-finished controller when a finished Job may be deleted. The timer starts when the Job reaches Complete or Failed. Once the TTL expires, the Job is deleted, and its dependent objects, such as its Pods, are deleted with it. The field is stable since Kubernetes v1.23.
What happens if I leave ttlSecondsAfterFinished unset?
The TTL controller does not clean up that Job. The Job stays in the API after it finishes until something else removes it. Finished Jobs left in the cluster put pressure on the API server, so set the field on any Job that runs repeatedly.
Does the TTL delete a Job that is still running?
No. The countdown starts only once the Job is Complete or Failed, so the TTL does not stop a running agent. Use activeDeadlineSeconds to bound how long a Job may run; when it passes, Kubernetes terminates the Pods and marks the Job Failed.
Should I set the TTL to 0 for agent Jobs?
Not if you need the logs. A value of 0 makes the Job eligible for deletion immediately after it finishes, and its Pods and their logs go with it. Choose a window long enough for your log collection and audit review to finish, then let the TTL run.