Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl list / delete all completed jobs

Tags:

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 ?

like image 582
should_be_working Avatar asked Nov 29 '18 12:11

should_be_working


People also ask

How do I delete all jobs from Kubernetes?

Delete the job with kubectl (e.g. kubectl delete jobs/pi or kubectl delete -f ./job. yaml ). When you delete the job using kubectl , all the pods it created are deleted too.

How do I delete a job on kubectl?

You can delete them at once with kubectl delete jobs --all , if you want to delete all jobs in the current namespace (not just the ones created by "hello".)

How do I delete failed jobs on Kubernetes?

To delete failed Jobs in GKE you will need to use following command: $ kubectl delete job $(kubectl get job -o=jsonpath='{. items[?(@. status.


2 Answers

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}')
like image 136
pcampana Avatar answered Oct 22 '22 03:10

pcampana


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

kubectl delete jobs --all-namespaces --field-selector status.successful=1 
like image 41
Ruelos Joel Avatar answered Oct 22 '22 02:10

Ruelos Joel