DEV Community

Maxime Guilbert
Maxime Guilbert

Posted on • Updated on

Automatically delete a Job in Kubernetes

When we work with Jobs in Kubernetes, we can need to make them run once we deploy and forget about them. So keep the resource is useless.

So, to do it, you just need to add the field .spec.ttlSecondsAfterFinished with the number of seconds you want to wait between the end of the Job and when it's deleted.


apiVersion: batch/v1
kind: Job
  name: pi-with-ttl
  ttlSecondsAfterFinished: 100
      - name: pi
        image: perl:5.34.0
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
Enter fullscreen mode Exit fullscreen mode

But be careful!! I've mentionned the end of the Job, so Kubernetes doesn't care if your Job ended successfully or not! It will be deleted either way! Consequently, be careful about the value of this field! If you haven't setup a log collector, you will see nothing about what happened if you have a too small value.

The best practice is to define a value big enough to let you connect manually to your cluster to check what happened.

If you are using Helm, we will see soon that there is a better system to manage this.


I hope it will help you! 🍺

You want to support me?

Buy Me A Coffee

Top comments (0)