<p>I can delete a deployment with the kubectl cli, but is there a way to make my deployment auto-destroy itself once it has finished? For my situation, we are kicking off a long-running process in a Docker container on AWS EKS. When I check the status, it is 'running', and then sometime later the status is 'completed'. So is there any way to get the kubernetes pod to auto destroy once it as finished?</p>

<pre><code>kubectl run some_deployment_name --image=path_to_image
kubectl get pods
  //the above command returns...
some_deployment_name1212-75bfdbb99b-vt622 0/1  Running 2  23s
  //and then some time later...
some_deployment_name1212-75bfdbb99b-vt622 0/1  Completed 2  15m
</code></pre>

<p>Once it is complete, I would like for it to be destroyed, without me having to call another command.</p>

MattC
<p>So the question is about running Jobs and not deployments as in the Kubernetes <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer">Deployments</a> abstraction that creates a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/" rel="nofollow noreferrer">ReplicaSet</a> but more like Kubernetes <a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/" rel="nofollow noreferrer">Jobs</a></p>

<p>A <code>Job</code> is created with <code>kubectl run</code> when you specify the <code>--restart=OnFailure</code> option.  These jobs are not cleaned up by the cluster unless you delete them manually with <code>kubectl delete &lt;pod-name&gt;</code>. More info <a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#clean-up-finished-jobs-automatically" rel="nofollow noreferrer">here</a>. </p>

<p>If you are using Kubernetes 1.12 or later a new Job spec was introduced: <code>ttlSecondsAfterFinished</code>. You can also use that to clean up your jobs.  Another more time-consuming option would be to write your own Kubernetes controller that cleans up regular Jobs.</p>

<p>A <code>CronJob</code> is created if you specify both the <code>--restart=OnFailure</code> and `--schedule="" option. These pods get deleted automatically because they run on a regular schedule.</p>

<p>More info on <code>kubectl run</code> <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run" rel="nofollow noreferrer">here</a>.</p>
