kubectl list / delete all completed jobs

should_be_working picture should_be_working · Nov 29, 2018 · Viewed 11.8k times · Source

I'm looking for a kubectl command to list / delete all completed jobs

I've try:

kubectl get job --field-selector status.succeeded=1

But I get:

enfield selector "status.succeeded=1": field label "status.succeeded" not supported for batchv1.Jobter code here

What are the possible fields for --fieldSelector when getting jobs ?

Is there a better way to do this ?

Answer

pcampana picture pcampana · Nov 29, 2018

What you can do to list all the succeeded jobs is first get all the jobs and then filter the output:

kubectl get job --all-namespaces | grep "succeeded"

If you want to delete all the succeded jobs you can use the following command:

kubectl delete job $(kubectl get job -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}')