kubernetes delete jobs by name

1/13/2022

I'm on a windows machine in a powershell prompt. I don't have the Linux commands like grep, awk.

I have a couple of jobs that I want to delete

enter image description here

I'm not finding the correct syntax for deleting them such as

kubectl delete job mailmigrationjob-id699*

or

kubectl delete job where name like mailmigrationjob-id699
-- CloudAnywhere
kubectl
kubernetes
powershell

2 Answers

1/13/2022

Try:

$Pattern="mailmigrationjob-id699"; & kubectl get job | % {"$_" -Split " "} | Select-String -Pattern $Pattern | %{ & kubectl delete job $_ }

Taken from: Delete all kubernetes pods by regex pattern with PowerShell

-- Kamol Hasan
Source: StackOverflow

1/14/2022

Another option:

> foreach($i in 1..10){kubectl create job mailmigrationjob-id699-$i --image=busybox -- echo "Hello World"}

job.batch/mailmigrationjob-id699-1 created
job.batch/mailmigrationjob-id699-2 created
job.batch/mailmigrationjob-id699-3 created
job.batch/mailmigrationjob-id699-4 created
job.batch/mailmigrationjob-id699-5 created
job.batch/mailmigrationjob-id699-6 created
job.batch/mailmigrationjob-id699-7 created
job.batch/mailmigrationjob-id699-8 created
job.batch/mailmigrationjob-id699-9 created
job.batch/mailmigrationjob-id699-10 created

> kubectl get jobs
NAME                        COMPLETIONS   DURATION   AGE
mailmigrationjob-id699-1    1/1           6s         9s
mailmigrationjob-id699-10   0/1           8s         8s
mailmigrationjob-id699-2    1/1           5s         9s
mailmigrationjob-id699-3    0/1           9s         9s
mailmigrationjob-id699-4    0/1           9s         9s
mailmigrationjob-id699-5    0/1           8s         8s
mailmigrationjob-id699-6    0/1           8s         8s
mailmigrationjob-id699-7    0/1           8s         8s
mailmigrationjob-id699-8    0/1           8s         8s
mailmigrationjob-id699-9    0/1           8s         8s

> $string="mailmigrationjob-id699"; $jobs=(kubectl get job -o NAME|Select-String $string); foreach ($job in $jobs){kubectl delete $job}     
job.batch "mailmigrationjob-id699-1" deleted
job.batch "mailmigrationjob-id699-10" deleted
job.batch "mailmigrationjob-id699-2" deleted
job.batch "mailmigrationjob-id699-3" deleted
job.batch "mailmigrationjob-id699-4" deleted
job.batch "mailmigrationjob-id699-5" deleted
job.batch "mailmigrationjob-id699-6" deleted
job.batch "mailmigrationjob-id699-7" deleted
job.batch "mailmigrationjob-id699-8" deleted
job.batch "mailmigrationjob-id699-9" deleted
-- Vit
Source: StackOverflow