Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes - delete all jobs in bulk

I can delete all jobs inside a custer running

kubectl delete jobs --all 

However, jobs are deleted one after another which is pretty slow (for ~200 jobs I had the time to write this question and it was not even done).

Is there a faster approach ?

like image 250
Overdrivr Avatar asked Apr 28 '17 08:04

Overdrivr


People also ask

How do I delete multiple jobs on Kubernetes?

To delete failed or long-running jobs: Show activity on this post. Show activity on this post. kubectl get jobs -o custom-columns=:. metadata.name gives you list of jobs name | then you can grep specific that you need with regexp | then xargs use output to delete one by one from the list.


Video Answer


3 Answers

It's a little easier to setup an alias for this bash command:

kubectl delete jobs `kubectl get jobs -o custom-columns=:.metadata.name`
like image 86
jaydeland Avatar answered Oct 06 '22 14:10

jaydeland


I have a script for deleting which was quite faster in deleting:

$ cat deljobs.sh 
set -x

for j in $(kubectl get jobs -o custom-columns=:.metadata.name)
do
    kubectl delete jobs $j &
done

And for creating 200 jobs used following script with the command for i in {1..200}; do ./jobs.sh; done

$ cat jobs.sh 
kubectl run memhog-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)  --restart=OnFailure --record --image=derekwaynecarr/memhog --command -- memhog -r100 20m
like image 31
surajd Avatar answered Oct 06 '22 12:10

surajd


If you are using CronJob and those are piling up quickly, you can let kubernetes delete them automatically by configuring job history limit described in documentation. That is valid starting from version 1.6.

...
  spec:
    ...
    successfulJobsHistoryLimit: 3
    failedJobsHistoryLimit: 3
like image 13
hurturk Avatar answered Oct 06 '22 12:10

hurturk