<p>I delete and re-submit a job with the same name, and I often get a 409 HTTP error with a message that says that the object is being deleted -- my submit comes before the job object is removed.</p>
<p>My current solution is to spin-try until I am able to submit a job.  I don't like it.  This looks quite ugly and I wonder if there's a way to call deletion routine in a way that waits till the object is completely deleted.  According to <a href="https://stackoverflow.com/a/52900505/18775">this</a>   kubectl waits till the object is actually deleted before returning from delete command.  I wonder if there's an option for the Python client.</p>
<p>Here's my spin-submit code (not runnable, sorry):</p>
<pre><code># Set up client
config.load_kube_config(context=context)
configuration = client.Configuration()
api_client = client.ApiClient(configuration)
batch_api = client.BatchV1Api(api_client)


job = create_job_definition(...)

batch_api.delete_namespaced_job(job.metadata.name, &quot;my-namespace&quot;)
for _ in range(50):
    try:
        return batch_api.create_namespaced_job(self.namespace, job)
    except kubernetes.client.rest.ApiException as e:
        body = json.loads(e.body)
        job_is_being_deleted = body[&quot;message&quot;].startswith(&quot;object is being deleted&quot;)
        if not job_is_being_deleted:
            raise
    time.sleep(0.05)

</code></pre>
<p>I wish it was</p>
<pre><code>batch_api.delete_namespaced_job(job.metadata.name, &quot;my-namespace&quot;, wait=True)
batch_api.create_namespaced_job(self.namespace, job)

</code></pre>
<p>I have found a similar question, and <a href="https://stackoverflow.com/a/65939132/18775">the answer suggests to use watch</a>, which means I need to start a watch in a separate thread, issue delete command, join the thread that waits till the deletion is confirmed by the watch -- seems like a lot of code for such a thing.</p>

<p>As you have already mentioned, kubectl delete has the <code>--wait</code> flag that does this exact job and is <code>true</code> by default.</p>
<p>Let's have a look at the code and see how kubectl implements this. <a href="https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete.go#L368-L378" rel="nofollow noreferrer">Source</a>.</p>
<pre><code>waitOptions := cmdwait.WaitOptions{
    ResourceFinder: genericclioptions.ResourceFinderForResult(resource.InfoListVisitor(deletedInfos)),
    UIDMap:         uidMap,
    DynamicClient:  o.DynamicClient,
    Timeout:        effectiveTimeout,

    Printer:     printers.NewDiscardingPrinter(),
    ConditionFn: cmdwait.IsDeleted,
    IOStreams:   o.IOStreams,
}
err = waitOptions.RunWait()
</code></pre>
<p>Additionally here are <a href="https://github.com/kubernetes/kubernetes/blob/d8f9e4587ac1265efd723bce74ae6a39576f2d58/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go#L233-L265" rel="nofollow noreferrer">RunWait()</a> and <a href="https://github.com/kubernetes/kubernetes/blob/d8f9e4587ac1265efd723bce74ae6a39576f2d58/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go#L268-L333" rel="nofollow noreferrer">IsDeleted()</a> function definitions.</p>
<p>Now answering your question:</p>
<blockquote>
<p>[...] which means I need to start a watch in a separate thread, issue delete command, join the thread that waits till the deletion is confirmed by the watch -- seems like a lot of code for such a thing</p>
</blockquote>
<p>It does look like so - it's a lot of code, but I don't see any alternative. If you want to wait for deletion to finish you need to do it manually. There does not seems to be any other way around it.</p>
