<p>I have more than 4000 pvc in my kubernetes aws eks cluster.</p>
<ul>
<li>From those 4000+ pvc I have to delete almost 3999 pvcs</li>
</ul>
<p>Now instead of using command:</p>
<pre><code>kubetcl delete pvc pvc-data-name
</code></pre>
<p>and delete every pvc separately which can take me sooo long.. I want to find a way to delete them in batches or all together.</p>

devgirl
<p>you can try this one.</p>
<pre><code>kubectl delete pvc --field-selector metadata.name!=&lt;name-dont-want-to-delete-1&gt;, metadata.name!=&lt;name-dont-want-to-delete-2&gt;, metadata.name!=&lt;name-dont-want-to-delete-3&gt;
</code></pre>
<p>or,
also you can do this with <code>go-client</code>. to do this with go client see the document <a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer">Ref</a></p>
<p>i have added a label in every <code>pvc</code> that i don't want to delete.</p>
<pre><code>  labels:
    test: test-1

</code></pre>
<pre><code>import (
   &quot;context&quot;
   &quot;fmt&quot;
   metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;

   &quot;k8s.io/client-go/kubernetes&quot;
   &quot;k8s.io/client-go/util/homedir&quot;
   &quot;kmodules.xyz/client-go/tools/clientcmd&quot;
   &quot;log&quot;
   &quot;path/filepath&quot;
)

func testFunc() ( error) {
   masterURL := &quot;&quot;

   kubeconfigPath := filepath.Join(homedir.HomeDir(), &quot;.kube&quot;, &quot;config&quot;)



   config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
   if err != nil {
       log.Fatalf(&quot;Could not get Kubernetes config: %s&quot;, err)
   }

   kc := kubernetes.NewForConfigOrDie(config)
   labelSelector := &amp;metav1.LabelSelector{
       MatchExpressions: []metav1.LabelSelectorRequirement{
           {
               &quot;test&quot;,
               metav1.LabelSelectorOpNotIn,
               []string{
                   &quot;test-0&quot;,
                   &quot;test-1&quot;,
               },
           },
       },
   }
   err = kc.CoreV1().PersistentVolumeClaims(&quot;default&quot;).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{
       LabelSelector: metav1.FormatLabelSelector(labelSelector),
   })
   fmt.Println(err)
   
   return  nil
}


</code></pre>
