<p>I want to delete all the Persistent Volume Claims attached to a customized deployment. At first, I delete the deployment itself that deletes all the pods but the disks aren't deleted.</p>
<p>I know the command to delete pvcs but that involves fetching all of them using a command[1] and then manually copying-pasting[2] the ones that belong to the deployment set.</p>
<p>If I want to do this programmatically, which API of Kubernetes Python client or the cli command do I use?</p>
<p>I am using GKE.</p>
<p>I tried reading the <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md" rel="nofollow noreferrer">Python client documentation</a> but could not deduce how to do this task.</p>
<p>[1]<code>kubectl --namespace=&lt;my_namespace&gt; --context=&lt;my_cluster&gt; get pvc</code></p>
<p>[2]<code>kubectl --namespace=&lt;my_namespace&gt; --context=&lt;cluster_name&gt; delete pvc data-avirals-july19-1-devel-0 data-avirals-july19-1-devel-1</code></p>

Aviral Srivastava
<p>There might be some shorter way, however this one would filter all the pvc associated with the deployment name <code>MY_DEPLOYMENT_NAME</code>. Other <code>pvc</code>(not associated with the same deployment) will not be touched.</p>
<pre><code>for volume in $(kubectl get deployments MY_DEPLOYMENT_NAME -o jsonpath='{.spec.template.spec.volumes[*].name}');
    do
      kubectl get pv $volume -o jsonpath='{.spec.claimRef.name}{&quot;\n&quot;}' ;
      # kubectl delete pvc &quot;$(kubectl get pv $volume -o jsonpath='{.spec.claimRef.name}{&quot;\n&quot;}')&quot;
done
</code></pre>
<p>Note:</p>
<ol>
<li><strong>uncomment</strong> the command <code>kubectl delete</code> to really delete the pvc. Its commented to protect users from accidently deleting.</li>
<li><code>bash</code> is required to make this loop work.</li>
</ol>
<p>Explanation:<br />
Following command would list down the volumes used by a deployment called <code>MY_DEPLOYMENT_NAME </code></p>
<pre><code>kubectl get deployments MY_DEPLOYMENT_NAME -o jsonpath='{.spec.template.spec.volumes[*].name}'
</code></pre>
<p>Now,we will loop over these volumes, to find what is the <code>claim</code> used by these volumes. This is done my checking <code>claimRef</code> section. This is done by:</p>
<pre><code>kubectl get pv $volume -o jsonpath='{.spec.claimRef.name}{&quot;\n&quot;}' 
</code></pre>
