<p>I am running this Cronjob at 2 AM in the morning:</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: postgres-backup
spec:
  # Backup the database every day at 2AM
  schedule: &quot;0 2 * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: postgres-backup
            image: postgres:10.4
            command:
            - &quot;/bin/sh&quot;
            - -c
            - |
              pg_dump -Fc -d postgresql://$DBUSER:$DBPASS@$DBHOST:$DBPORT/$DBNAME &gt; /var/backups/backup_$(date +&quot;%d-%m-%Y_%H-%M&quot;).bak;
            env:
            - name: DBHOST
              valueFrom:
                configMapKeyRef:
                  name: dev-db-config
                  key: db_host
            - name: DBPORT
              valueFrom:
                configMapKeyRef:
                  name: dev-db-config
                  key: db_port
            - name: DBNAME
              valueFrom:
                configMapKeyRef:
                  name: dev-db-config
                  key: db_name
            - name: DBUSER
              valueFrom:
                secretKeyRef:
                  name: dev-db-secret
                  key: db_username
            - name: DBPASS
              valueFrom:
                secretKeyRef:
                  name: dev-db-secret
                  key: db_password
            volumeMounts:
            - mountPath: /var/backups
              name: postgres-backup-storage
          - name: postgres-restore
            image: postgres:10.4
            volumeMounts:
            - mountPath: /var/backups
              name: postgres-backup-storage
          restartPolicy: OnFailure
          volumes:
          - name: postgres-backup-storage
            hostPath:
            # Ensure the file directory is created.
              path: /var/volumes/postgres-backups
              type: DirectoryOrCreate
</code></pre>
<p>The jobs are getting executed successfully, but what I don't like is that for every Job execution a new Pod is created:</p>
<p><a href="https://i.stack.imgur.com/42w3e.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/42w3e.png" alt="Multiple Pods of Cornjob Job execution" /></a></p>
<p>Is there a way to clean previous (old) created Pods?
Or maybe is there a way to rerun one an the same Pod/Job every time?</p>

<p>If only last job and pod need to be preserved, you can use <code>.spec.successfulJobsHistoryLimit</code> field set to <code>1</code>.</p>
<p>This way only last job and corresponding pod will be preserved. By default it's set to <code>3</code>. Also it's possible to set this value to <code>0</code> and nothing will be saved after cronjob's execution.</p>
<p>Same logic has <code>.spec.failedJobsHistoryLimit</code> field, it has <code>1</code> by default.</p>
<p>See <a href="https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#jobs-history-limits" rel="nofollow noreferrer">jobs history limits</a>.</p>
<hr />
<p>This is how it looks when I get events from cronjob:</p>
<pre><code>$ kubectl describe cronjob test-cronjob
...
Events:
  Type    Reason            Age    From                Message
  ----    ------            ----   ----                -------
  Normal  SuccessfulCreate  2m31s  cronjob-controller  Created job test-cronjob-27304493
  Normal  SawCompletedJob   2m30s  cronjob-controller  Saw completed job: test-cronjob-27304493, status: Complete
  Normal  SuccessfulCreate  91s    cronjob-controller  Created job test-cronjob-27304494
  Normal  SawCompletedJob   90s    cronjob-controller  Saw completed job: test-cronjob-27304494, status: Complete
  Normal  SuccessfulDelete  90s    cronjob-controller  Deleted job test-cronjob-27304493
  Normal  SuccessfulCreate  31s    cronjob-controller  Created job test-cronjob-27304495
  Normal  SawCompletedJob   30s    cronjob-controller  Saw completed job: test-cronjob-27304495, status: Complete
  Normal  SuccessfulDelete  30s    cronjob-controller  Deleted job test-cronjob-27304494
</code></pre>
<p>Only one last job is presented:</p>
<pre><code>$ kubectl get jobs
NAME                    COMPLETIONS   DURATION   AGE
test-cronjob-27304496   1/1           1s         3s
</code></pre>
<p>And one pod:</p>
<pre><code>$ kubectl get pods
NAME                               READY   STATUS      RESTARTS   AGE
test-cronjob-27304496-r4qd8        0/1     Completed   0          38s
</code></pre>
