0

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

From those 4000+ pvc I have to delete almost 3999 pvcs and not delete like 30 pvc.

I will be using the command:

kubectl delete pvc --field-selector metadata.name!=<name not to delete>

to test before I will be using

kubectl get pvc --field-selector metadata.name!=<name not to delete>
  • now when i use the command and add the pvc name of only ONE PVC in place of "name not to delete" the command works.. but when I add another pvc name rather than ONE like "kubectl get pvc --field-selector metadata.name!=pvc1 pvc2" .. it does NOT work

as I mentioned I do not want to delete 30 pvc but the rest 4000 I do want to delete... so I need to add those 30 pvc in front of the command. So I need help regarding that.

  • Now I also want to try to select resources in bacth by LABELS of AGE.. I only want to select pvc which are more than 2 days old.. how do I do that?
David Maze
  • 130,717
  • 29
  • 175
  • 215
devgirl
  • 671
  • 3
  • 16
  • 39
  • In kubernetes it is much easier to select sets/batches of things by using Labels - that is how most things in Kubernetes is designed. – Jonas Jan 31 '21 at 05:10
  • @Jonas yes u are right.. but how do I add a label selection of AGE ( i only want pvc's which are more than 2 days old.) – devgirl Jan 31 '21 at 06:35
  • _"Now I also want to try to select resources in bacth by LABELS of AGE.. I only want to select pvc which are more than 2 days old.. how do I do that?"_ if your initial question has already been answered in this thread and the answer accepted, please post a new question instead of editing this one. – mario Feb 01 '21 at 15:09

2 Answers2

2

You can try

kubectl get pvc --field-selector metadata.name!=pvc1,metadata.name!=pvc2

See https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get

iii
  • 102
  • 4
0

Although a bit too much for the task yet you can use something like below that also prints the name for each PVC before deleting just to be sure:

_contains () {  
  echo "$1" | tr ' ' '\n' | grep -F -x -q "$2"
}

deleteList="claim-http-0 claim-http-1"

for each in $(kubectl get pvc -o jsonpath="{.items[*].metadata.name}" | tr " " "\n");
do
  if _contains "${deleteList}" "${each}"; then
    echo $each
    kubectl delete pvc $each
  fi
done
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35