Kubernetes – Delete All Completed Jobs One-Liner

Kubernetes Logo

Sometimes you forget, or don’t want to add, successfulJobsHistoryLimit or event perhaps failedJobsHistoryLimit. Whatever the case, the end result looks something like this:

NAME                           COMPLETIONS   DURATION   AGE
backup-one-27742920            1/1           7s         13h
backup-two-27742920            1/1           8s         13h
backup-three-27742920          1/1           8s         13h
backup-four-27742920           1/1           8s         13h
backup-five-27742920           1/1           10s        13h
restart-pod-27742920           1/1           24s        13h

You can of course go and kubectl delete all of them, but a much quicker solution is using this one-liner:

kubectl delete jobs `kubectl get jobs -o custom-columns=:.metadata.name`

Running the above one-line will list all the jobs in the default namespace and remove them.

You can even create a ZSH or BASH alias for even quicker access:

alias kjobdel='kubectl delete jobs `kubectl get jobs -o custom-columns=:.metadata.name`'

Three cheers for avoiding best practices! 👾

Leave a Reply

Your email address will not be published. Required fields are marked *