31

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 ?

should_be_working
  • 699
  • 1
  • 7
  • 11

4 Answers4

35

you are almost there, you can do below to delete completed jobs

kubectl delete jobs --all-namespaces --field-selector status.successful=1 
user672009
  • 4,379
  • 8
  • 44
  • 77
Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33
  • 3
    great, do you have docs why this works? I only found "Supported field selectors vary by Kubernetes resource type" in https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ and given that jobs have `status.succeeded: 1` in their yaml it is not very intuitive (although it works). – dastrobu Jan 15 '21 at 14:25
  • 1
    Little bit late, but I eventually managed to track down where these are defined https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#job-v1-batch – dann.dev Jan 27 '21 at 23:12
31

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}')
pcampana
  • 2,413
  • 2
  • 21
  • 39
  • 1
    The first command does not work. We might have different kubectl version. The second command work great ! Thanks ! – should_be_working Nov 29 '18 at 14:37
  • 2
    The first command does not work as @should_be_working pointed out. That's because there is no field that describes the status of a job as "succeeded" in the default `kubectl get job` list. – jwadsack Jun 12 '19 at 20:55
  • If working with powershell, the command to list all the succeeded jobs is: `kubectl get job --all-namespaces | select-string "succeeded"` – pcampana Dec 13 '19 at 08:55
  • I think it's beneficial to stress that in this answer the filtering is obtained by the Filtering expression `[?(expression)]`. At first, it was not obvious to me how an output template could act as a filter. In filter expressions the `@` is the current node being processed. More on Filter Expression here: https://rows.com/docs/filtering-with-jsonpath#filtering – Alessandro Dentella Aug 09 '22 at 06:50
8

FWIW, the following returns all jobs that have failed:

kubectl get jobs $(kubectl get jobs -o=jsonpath='{.items[?(@.status.failed>0)].metadata.name}')

And all jobs that are still running:

kubectl get jobs $(kubectl get jobs -o=jsonpath='{.items[?(@.status.active==1)].metadata.name}')
jwadsack
  • 5,708
  • 2
  • 40
  • 50
4

More elegant and shorter version:

kubectl delete pod --field-selector=status.phase==Succeeded
Vasili Angapov
  • 8,061
  • 15
  • 31