7

I tried to delete my jobs with a LabelSelector by client-go:

cli.BatchV1().Jobs("default").Delete(context.TODO(), name, metav1.DeleteOptions{})

And the job was deleted successfully, but the pods of it didn't!

If I delete this job by kubectl, the pod it created would be deleted automatically.

How could I delete jobs with its pods simply by client-go?

peterh
  • 11,875
  • 18
  • 85
  • 108
Reed Chan
  • 525
  • 6
  • 16
  • 1
    Somehow you need to enable cascade deletion or DeletePropagation to delete child objects like pods. – Vasili Angapov Jan 29 '21 at 08:14
  • 3
    Yes, I found that what I need to do was set `PropagationPolicy` in `DeleteOptions`. Now when I delete a job the dependents would be deleted automatically. Thanks. – Reed Chan Jan 29 '21 at 10:10
  • 1
    sounds like we know an answer to this question we haven't provided to the page – erik258 Jan 30 '21 at 18:13

1 Answers1

1

you need to set the PropagationPolicy field in DeleteOptions to Background. This ensures that the Job and its child Pods are deleted.

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

//...

backgroundDeletion := metav1.DeletePropagationBackground

err := cli.BatchV1()
       .Jobs("default")
       .Delete(context.TODO(), name, metav1.DeleteOptions{
           PropagationPolicy: &backgroundDeletion, 
       })
dom1
  • 425
  • 1
  • 2
  • 19
  • 1
    https://github.com/kubernetes/client-go/issues/495 this issue can also help you with. Here are the docs! https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#DeleteOptions – Rômulo M. Farias Jun 22 '23 at 06:38