0

I have more than 4000 pvc in my kubernetes aws eks cluster.

  • From those 4000+ pvc I have to delete almost 3999 pvcs

Now instead of using command:

kubetcl delete pvc pvc-data-name

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.

David Maze
  • 130,717
  • 29
  • 175
  • 215
devgirl
  • 671
  • 3
  • 16
  • 39
  • 1
    `kubectl delete pvc --field-selector metadata.name!=`. you can test with `kubectl get` before changing to `kubectl delete`. – jordanm Jan 29 '21 at 20:22
  • i personally tried python kubernetes client and it worked like 10 times faster than kubectl when deleting tons of objects – Vasili Angapov Jan 29 '21 at 20:24
  • @VasiliAngapov and how did u do that? – devgirl Jan 31 '21 at 03:22
  • @jordanm thanks.. I will try it out! However, just to clarify: I dont have to delete like 30 pvc so in the "metadata.name!=" that u have in the command do I add the names of all those 30? – devgirl Jan 31 '21 at 03:24
  • @jordanm for example.. like if I have 5 pvc data1, data2, data3, data4, data5 ... how do add all infront of the "metadata.name!="?? – devgirl Jan 31 '21 at 04:02

2 Answers2

3

you can try this one.

kubectl delete pvc --field-selector metadata.name!=<name-dont-want-to-delete-1>, metadata.name!=<name-dont-want-to-delete-2>, metadata.name!=<name-dont-want-to-delete-3>

or, also you can do this with go-client. to do this with go client see the document Ref

i have added a label in every pvc that i don't want to delete.

  labels:
    test: test-1

import (
   "context"
   "fmt"
   metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

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

func testFunc() ( error) {
   masterURL := ""

   kubeconfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config")



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

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


Emon46
  • 1,506
  • 7
  • 14
  • Emon thank you.. I will try it out! However, just to clarify: I dont have to delete like around 30 pvc so in the "metadata.name!=" that u have in the command do I add the names of all those 30? – devgirl Jan 31 '21 at 03:24
  • Emon for example, like if I have 5 pvc data1, data2, data3, data4, data5 ... how do add all infront of the "metadata.name!="?? – devgirl Jan 31 '21 at 04:01
  • @Myra For keeping multiple pvc you better try the second one. – Emon46 Jan 31 '21 at 10:05
  • @Myra i have updated my ans . can you try the `kubectl` one? – Emon46 Jan 31 '21 at 16:18
  • Emon sure.. but i am facing another problem..when typing the command on my terminal (it has like 40 pvc that I dont want to delete) it is not pasting the full command? how do I fix that – devgirl Feb 01 '21 at 06:14
  • i haven't get it. can you explain a little more? – Emon46 Feb 01 '21 at 06:36
  • Emon pls see this: https://stackoverflow.com/questions/65988112/mac-terminal-command-input-too-long-it-does-not-fit – devgirl Feb 01 '21 at 06:38
1

Try this one with grep and xargs

kubectl get pvc | grep -v <name-to-not-include> | xargs kubectl delete pvc --grace-period=0 --force

Update:

For multiple patterns:

kubectl get pvc | grep -v 'pattern1\|pattern2' | xargs kubectl delete pvc --grace-period=0 --force
paltaa
  • 2,985
  • 13
  • 28