39

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 ?

1
  • 1
    You would have to query the api, create a list from that query, and run all your commands as background job. That should be faster (however you will not know until tested)
    – Norbert
    Apr 28, 2017 at 19:26

12 Answers 12

56

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

kubectl delete jobs `kubectl get jobs -o custom-columns=:.metadata.name`
2
  • Expanding this answer: To set up an alias add this line to your .bashrc or .zshrc file: alias delete_all='kubectl delete jobs `kubectl get jobs -o custom-columns=:.metadata.name`'
    – emil
    Jun 23, 2022 at 6:23
  • Please use this notation kubectl delete jobs $(kubectl get jobs -o custom-columns=:.metadata.name) as backticks have several issues: 1. It has a series of undefined behaviors related to quoting in POSIX. 2. It imposes a custom escaping mode with surprising results. 3. It's exceptionally hard to nest. I recommend www.shellcheck.net
    – Alakdae
    May 29 at 7:28
24

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
4
  • 2
    Thanks for the answer, I'll try this asap
    – Overdrivr
    May 5, 2017 at 7:25
  • you can do kubectl -n <namespace name> delete ...
    – surajd
    Jul 6, 2017 at 13:30
  • 4
    and with & and >16k jobs it will kill your computer Jul 6, 2017 at 14:37
  • I remove the & if there are too many jobs to remove, it's slower but safer indeed. But otherwise this does the job
    – Overdrivr
    Dec 7, 2017 at 10:38
14

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
2
  • 1
    Thanks! I was "playing" with cronjobs and had piled up 17K+. Deleting all of this is a pain. Lesson learned:)
    – Kuberchaun
    May 18, 2017 at 2:24
  • Note that the limits have somewhat reasonable defaults of 3 successful and 1 failed jobs.
    – Ilkka
    Jan 14, 2019 at 8:11
10

There is an easier way to do it:

To delete successful jobs:

kubectl delete jobs --field-selector status.successful=1

To delete failed or long-running jobs:

kubectl delete jobs --field-selector status.successful=0

6

This works really well for me:

kubectl delete jobs $(kubectl get jobs -o custom-columns=:.metadata.name)

3
  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.
    – Dwhitz
    Aug 8, 2019 at 6:42
  • @Cizer I want to get the list of job names that match with the grep statement. kubectl get jobs | grep abc-xyt gives the output along with status, and time. I used kubectl get jobs --output=jsonpath='{.items[*].metadata.name}' | egrep abc-xyt where it fails miserabily to grep the job names which matches with abc-xyt. Any idea?
    – rishi
    Aug 9, 2022 at 7:22
  • @rishi Using custom column: kubectl get jobs -o custom-columns=Name:.metadata.name,:.status.startTime | grep nginx Would also work for bulk delete: kubectl delete jobs $(kubectl get jobs -o custom-columns=:.metadata.name | grep "nginx-old") Oct 5, 2022 at 16:07
4

I use this script, it's fast but it can trash CPU (a process per job), you can always adjust the sleep parameter:

#!/usr/bin/env bash

echo "Deleting all jobs (in parallel - it can trash CPU)"

kubectl get jobs --all-namespaces | sed '1d' | awk '{ print $2, "--namespace", $1 }' | while read line; do
  echo "Running with: ${line}"
  kubectl delete jobs ${line} &
  sleep 0.05
done
3

The best way for me is (for completed jobs older than a day):

kubectl get jobs | grep 1/1 | gawk 'match($0, / ([0-9]*)h/, ary) { if(ary[1]>24) print $1}' | parallel -r --bar -P 32 kubectl delete jobs

grep 1/1 for completed jobs

gawk 'match($0, / ([0-9]*)h/, ary) { if(ary[1]>24) print $1}' for jobs older than a day

-P number of parallel processes

It is faster than kubectl delete jobs --all, has a progress bar and you can use it when some jobs are still running.

2

kubectl delete jobs --all --cascade=false is fast, but won't delete associated resources, such as Pods

https://github.com/kubernetes/kubernetes/issues/8598

1

Parallelize using GNU parallel

parallel --jobs=5 "echo {}; kubectl delete jobs {} -n core-services;" ::: $(kubectl get job -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}'  -n core-services)
1

kubectl get jobs -o custom-columns=:.metadata.name | grep specific* | xargs kubectl delete jobs

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.

1
  • 1
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
    – Yunnosch
    May 3, 2021 at 6:57
0

Probably, there's no other way to delete all job at once,because even kubectl delete jobs also queries one job at a time, what Norbert van Nobelen suggesting might get faster result, but it will make much difference.

0

Kubectl bulk (bulk-action on krew) plugin may be useful for you, it gives you bulk operations on selected resources. This is the command for deleting jobs ' kubectl bulk jobs delete '

You could check details in https://github.com/emreodabas/kubectl-plugins/blob/master/README.md#kubectl-bulk-aka-bulk-action

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.