<p>I have a YAML file which creates a pod on execution. This pod extracts data from one of our internal systems and uploads to GCP. It takes around 12 mins to do so after which the status of the pod changes to 'Completed', however I would like to delete this pod once it has completed.</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: xyz
spec:
  restartPolicy: Never
  volumes:
  - name: mount-dir
    hostPath:
      path: /data_in/datos/abc/
  initContainers:
   - name: abc-ext2k8s
     image: registrysecaas.azurecr.io/secaas/oracle-client11c:11.2.0.4-latest
     volumeMounts:
     - mountPath: /media
       name: mount-dir
     command: ["/bin/sh","-c"] 
     args: ["sqlplus -s CLOUDERA/MYY4nGJKsf@hal5:1531/dbmk @/media/ext_hal5_lk_org_localfisico.sql"]
  imagePullSecrets:
   - name: regcred
</code></pre>

<p>Is there a way to acheive this?</p>

Sakshi Mittal
<p>Typically you don't want to create bare Kubernetes pods.  The pattern you're describing of running some moderate-length task in a pod, and then having it exit, matches a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/" rel="nofollow noreferrer">Job</a>.  (Among other properties, a job will reschedule a pod if the node it's on fails.)</p>

<p>Just switching this to a Job doesn't directly address your question, though.  <a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#job-termination-and-cleanup" rel="nofollow noreferrer">The documentation notes</a>:</p>

<blockquote>
  <p>When a Job completes, no more Pods are created, but the Pods are not deleted either. Keeping them around allows you to still view the logs of completed pods to check for errors, warnings, or other diagnostic output. The job object also remains after it is completed so that you can view its status. It is up to the user to delete old jobs after noting their status.</p>
</blockquote>

<p>So whatever task creates the pod (or job) needs to monitor it for completion, and then delete the pod (or job).  (Consider using the <a href="https://kubernetes.io/docs/reference/using-api/api-concepts/#efficient-detection-of-changes" rel="nofollow noreferrer">watch API</a> or equivalently the <code>kubectl get -w</code> option to see when the created objects change state.)  There's no way to directly specify this in the YAML file since there is a specific intent that you can get useful information from a completed pod.</p>

<p>If this is actually a nightly task that you want to run at midnight or some such, you do have one more option.  A <a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/" rel="nofollow noreferrer">CronJob</a> will run a job on some schedule, which in turn runs a single pod.  The important relevant detail here is that <a href="https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#jobs-history-limits" rel="nofollow noreferrer">CronJobs have an explicit control for how many completed Jobs they keep</a>.  So if a CronJob matches your pattern, you can set <code>successfulJobsHistoryLimit: 0</code> in the CronJob spec, and created jobs and their matching pods will be deleted immediately.</p>
