<p>How to delete the failed jobs in the kubernetes cluster using a cron job in gke?. when i tried to delete the failed jobs using following YAML, it has deleted all the jobs (including running)</p>
<pre><code>
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: XXX
namespace: XXX
spec:
schedule: &quot;*/30 * * * *&quot;
failedJobsHistoryLimit: 1
successfulJobsHistoryLimit: 1
jobTemplate:
 spec:
   template:
     spec:
       serviceAccountName: XXX
       containers:
       - name: kubectl-runner
         image: bitnami/kubectl:latest
         command: [&quot;sh&quot;, &quot;-c&quot;, &quot;kubectl delete jobs $(kubectl get jobs | awk '$2 ~ 1/1' | awk '{print $1}')&quot;]
       restartPolicy: OnFailure
</code></pre>

<p>To delete failed <strong><code>Jobs</code></strong> in <code>GKE</code> you will need to use following command:</p>
<ul>
<li><code>$ kubectl delete job $(kubectl get job -o=jsonpath='{.items[?(@.status.failed==1)].metadata.name}')</code></li>
</ul>
<p>This command will output the <code>JSON</code> for all jobs and search for jobs that have <code>status.failed</code> field set to <code>1</code>. It will then pass the failed jobs to <code>$ kubectl delete jobs</code></p>
<hr />
<p><strong>This command ran in a <code>CronJob</code>  will fail when there are no jobs with status: <code>failed</code>.</strong></p>
<p>As a <strong>workaround</strong> you can use:</p>
<pre class="lang-yaml prettyprint-override"><code>command: [&quot;sh&quot;, &quot;-c&quot;, &quot;kubectl delete job --ignore-not-found=true $(kubectl get job -o=jsonpath='{.items[?(@.status.failed==1)].metadata.name}'); exit 0&quot;]
</code></pre>
<blockquote>
<p><code>exit 0</code> was added to make sure that the <code>Pod</code> will leave with status code 0</p>
</blockquote>
<hr />
<p>As for part of the comments made under the question:</p>
<blockquote>
<p>You will need to modify it it support <strong>&quot;Failed&quot;</strong> Jobs</p>
</blockquote>
<blockquote>
<p>I have already tried the following , but it's not deleting the jobs. <code>kubectl delete job $(kubectl get job -o=jsonpath='{.items[?(@.status.Failed==1)].metadata.name}')</code></p>
</blockquote>
<ul>
<li><code>@.status.Failed==1</code> &lt;-- incorrect as <code>JSON</code> is <a href="https://www.quora.com/Is-JSON-case-sensitive" rel="nofollow noreferrer">case sensitive</a></li>
<li><code>@.status.failed==1</code> &lt;-- correct</li>
</ul>
<p>If you were to run incorrect version of this command on following Pods (to show that they failed and aren't still running to completion):</p>
<pre><code>NAME              READY   STATUS      RESTARTS   AGE
job-four-9w5h9    0/1     Error       0          5s
job-one-n9trm     0/1     Completed   0          6s
job-three-nhqb6   0/1     Error       0          5s
job-two-hkz8r     0/1     Error       0          6s
</code></pre>
<p>You should get the following error :</p>
<pre><code>error: resource(s) were provided, but no name, label selector, or --all flag specified
</code></pre>
<blockquote>
<p>Above error will also  show when there was no jobs passed to <code>$ kubectl delete job</code>.</p>
</blockquote>
<p>Running correct version of this command should delete all jobs that failed:</p>
<pre><code>job.batch &quot;job-four&quot; deleted
job.batch &quot;job-three&quot; deleted
job.batch &quot;job-two&quot; deleted
</code></pre>
<hr />
<p>I encourage you to check additional resources:</p>
<ul>
<li><em><a href="https://stackoverflow.com/questions/53539576/kubectl-list-delete-all-completed-jobs">StackOverflow.com: Questions: Kubectl list delete all completed jobs</a></em></li>
<li><em><a href="https://kubernetes.io/docs/reference/kubectl/jsonpath/" rel="nofollow noreferrer">Kubernetes.io: Doc</a></em></li>
</ul>
