Adrien Besnard
10/15/2025, 12:57 PMMarvin
10/15/2025, 12:57 PMMarvin
10/15/2025, 1:03 PMprefect work-pool inspect "my-k8s-pool"
2) Save and edit the base job template JSON to include ttlSecondsAfterFinished under job.spec:
{
"job": {
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": {
"labels": {
"<http://prefect.io/work-pool|prefect.io/work-pool>": "my-k8s-pool"
}
},
"spec": {
"ttlSecondsAfterFinished": 0,
"backoffLimit": 0,
"template": {
"spec": {
"restartPolicy": "Never",
"terminationGracePeriodSeconds": 5,
"containers": [
{
"name": "prefect-job",
"image": "your-image:tag"
}
]
}
}
}
}
}
3) Update the work pool with your edited template:
prefect work-pool create "my-k8s-pool" --type kubernetes --base-job-template ./base-job-template.json --overwrite
Notes and tips
- The TTL controller is enabled by default on modern Kubernetes versions. If your cluster is old or managed differently, verify it’s enabled.
- When you click “Cancel” in Prefect, the Kubernetes job should terminate; TTL then removes the Job (and the Pods via ownerReferences/GC). Setting a small terminationGracePeriodSeconds helps jobs exit quickly.
- There isn’t a Prefect 3.x flag like keep_job to flip this behavior; the recommended pattern is to manage cleanup via the Kubernetes Job spec.
- If you prefer some delay for troubleshooting, set ttlSecondsAfterFinished to a few minutes instead of 0.
Docs
- Deploying to Kubernetes (Prefect 3)
If you’re already doing this and still see Jobs sticking around after cancel, let me know:
- Your Prefect version
- Kubernetes version
- The work pool’s base job template snippet
- Whether you want immediate deletion or a short grace period for debugging logs