<p>When deleting a pod manually <code>kubectl delete</code> wait for the pod to be deleted and one can include a <code>kubectl wait --for....</code> condition in a script to wait for the pod to be deleted.</p>
<p>I would like to perform the same wait condition but when scaling down (replicas: 0) a deployment.</p>
<p>From the deployment json, the available/unavailable replicas doesn't count the &quot;terminating&quot; pods and as expected <code>kubectl wait --for=delete deployment test-dep</code> doesn't wait for pod termination but for deployment deletion.</p>
<p>So I would like to perform on my script:</p>
<pre><code>kubectl scale --replicas=0 deployment foo-bar
kubectl wait --for=deletion-of-pod-from-deploymenent=foo-bar
</code></pre>
<p>Is there a way to do that?</p>
<p>Remarks: I would like to have the code as generic as possible so no hard writing the labels from deployment.</p>

<p>The easiest way would be to use labels and issue <code>kubectl wait</code> based on that.</p>
<pre class="lang-text prettyprint-override"><code>kubectl wait --for delete pod --selector=&lt;label&gt;=&lt;value&gt;
</code></pre>
<p><em>but</em>, since you don't want that, you can use the script below</p>
<pre class="lang-sh prettyprint-override"><code>#!/bin/bash

deployment_uid=$(kubectl get deployments ${1} -o=jsonpath='{.metadata.uid}')
rs_uids=$(kubectl get rs -o=json | jq -r '.items[] | select(.metadata.ownerReferences[].uid=='\&quot;${deployment_uid}\&quot;') | .metadata.uid')

PODS=&quot;&quot;

for i in $rs_uids; do
    PODS+=$(kubectl get pods -o=json | jq -r '.items[] | select(.metadata.ownerReferences[].uid=='\&quot;$i\&quot;') | &quot;pod/&quot; + .metadata.name')
done

[ -z &quot;$PODS&quot; ] &amp;&amp; echo &quot;Pods not found&quot; || kubectl wait --for=delete --timeout=-1s ${PODS}
</code></pre>
<p>This uses Deployment name (as a first argument), to get ownerRefereces UIDs, chaining down to Pods names.</p>
<p>It's much more complicated, and prone to failure, than just using labels.</p>
