-1

I want to delete all PersistentVolumeClaim in Kubernetes that have claim word in their name.

for example: claim-me-dot-com

How can I do it? thanks.

Nader
  • 318
  • 1
  • 6
  • 16
  • Try this... https://stackoverflow.com/questions/57401526/how-to-delete-persistent-volumes-in-kubernetes YOU should check current namespace. – J. Song Dec 05 '21 at 07:46
  • @J.Song I check it. it won't solve my problem. thanks. – Nader Dec 05 '21 at 07:54
  • `kubectl get pvc -n nextera -o name | cut -d/ -f2 | grep claim better answer – nader 1 min ago` – gohm'c Dec 05 '21 at 08:04

3 Answers3

2

You can use this to delete all PVCs whose names contain claim. You should check every namespace and then delete the PVC in that namespace. Otherwise, you may delete important PVCs.

export NAMESPACE=your-name-space
export DELETE_PHRASE=claim
kubectl get pvc -n NAMESPACE --no-headers=true | awk '{ print $2 }' | grep $DELETE_PHRASE | xargs kubectl delete pvc -n $NAMESPACE

To delete in bulk, you can use this for script to delete all of the PVCs. Use this at your own RISK

for ns in `kubectl get ns --no-headers=true -o custom-columns=":metadata.name"`; 
    for pvc in `kubectl get pvc -n $ns --no-headers=true -o custom-columns=":metadata.name" | grep claim`;
        kubectl delete pvc $pvc -n $ns;
    done
done
Numb95
  • 103
  • 2
2

The solution below is restricted to the current namespace:

kubectl get pvc -o name | grep "claim-me-dot-com" | xargs -n 1 kubectl delete

To delete pvc from all namespaces:

kubectl get pvc -o name -A | grep "claim-me-dot-com" | xargs -n 1 kubectl delete
1

Another way would be to delete multiple objects based on their labels.

e.g.

kubectl delete pvc -l 'app.kubernetes.io/component in (<FIRST_VALUE>, <SECOND_VALUE>)'
Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73