We don't want to delete PV and PVC as pods reuse them most of the times. However, in the long term, we end up with many PVs' and PVCs' that are not used. How to safely clean?
6 Answers
Not very elegant but bash way to delete Released PV's
kubectl get pv | grep Released | awk '$1 {print$1}' | while read vol; do kubectl delete pv/${vol}; done

- 2,548
- 2
- 19
- 29
Looking through the current answers it looks like most of these don't directly answer the question (I could be mistaken). A PVC that is Bound
is not the same as Mounted
. The current answers should suffice to clean up Unbound
PVC's, but finding and cleaning up all Unmounted
PVC's seems unanswered.
Unfortunately it looks like the -o=go-template=...
doesn't have a variable for Mounted By:
as shown in kubectl describe pvc
.
Here's what I've come up with after some hacking around:
To list all PVC's in a cluster (mounted and not mounted) you can do this: kubectl describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Mounted By:.*$"
The -A
will return every PVC in the cluster in every namespace. We then filter down to show just the Name
, Namespace
and Mounted By
fields.
The best I could come up with to then get the names and namespaces of all unmounted PVC's is this:
kubectl describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Mounted By:.*$" | grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*$"
To actually delete the PVC's is somewhat difficult because we need to know the name of the PVC as well as it's namespace. We use cut, paste and xargs to do this:
kubectl describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Mounted By:.*$" | grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*$" | cut -f2 -d: | paste -d " " - - | xargs -n2 bash -c 'kubectl -n ${1} delete pvc ${0}'
cut
removesName:
andNamespace:
since they just get in the waypaste
puts theName
of the PVC and it'sNamespace
on the same linexargs -n bash
makes it so the PVC name is${0}
and the namespace is${1}
.
I admit that I have a feeling that this isn't the best way to do this but it was the only obvious way I could come up with (on the CLI) to do this.
After running this your volumes will go from Bound
to Unbound
and the other answers in this thread have good ideas on how to clean those up.
Also, keep in mind that some of the volume controllers don't actually delete your data when the volumes are deleted in Kubernetes. You might still need to clean that up in whichever system you are using.
For example, in the NFS controller the data gets renamed with an archived-
prefix and on the NFS side you can run rm -rf /persistentvolumes/archived-*
. For AWS EBS you might still need to delete the EBS volumes if they are detached from any instance.
I hope this helps!

- 669
- 5
- 13
-
1super super super answer! I am so grateful! But the only difference I made is using "Used By" instead of "Mounted By". That's all! Thanks again! – Başar Söker Jun 02 '22 at 18:58
If you'd like to remove all the Unbound PVs and PVCs, you can do this:
First delete the PVCs:
$ kubectl -n <namespace> get pvc | tail -n +2 | grep -v Bound | \
awk '{print $1}' | xargs -I{} kubectl -n namespace delete pvc {}
Then just delete the PVs:
$ kubectl -n <namespace> get pv | tail -n +2 | grep -v Bound | \
awk '{print $1}' | xargs -I{} kubectl -n namespace delete pv {}

- 58,485
- 12
- 111
- 141
-
don't think it's a right syntax or option. even if I set -o wide to get more details all my pvc's stay bound. I was hopping for something like ```docker system prune```. OR feels like I need to connect via: if pvc['Mounted By"]=None -> delete – Pav K. Nov 08 '18 at 21:02
-
```kubectl get pvc -o name``` only returns name. it doesn't return details. you probably meant ```-o wide```. – Pav K. Nov 08 '18 at 21:05
-
I didn't mean wide... no output option., you can get whether a PV and PVC is bound with the regular get pv or pvc output. Saw that you change the question though. – Rico Nov 08 '18 at 21:07
-
all my PVC's have bound status (mounted and unmounted). Feels like I need to connect via: if pvc['Mounted By"]=None -> delete. I guess I have to read up how to get it via templates – Pav K. Nov 08 '18 at 21:10
-
You can probably do that with `-o=jsonpath`. Can you post a sample for mounted and unmounted? – Rico Nov 08 '18 at 21:15
All previous answsers are valid and interesting. Here is another simple way to delete persistent volumes.
You should first delete your associated persistentvolumeclaim but in some cases the persistentvolumes could not be deleted automaticaly. (Ex : a "Retain" reclaim policy).
Here is a safe syntax for persistentvolumes deletion with Released satus (unused and unmounted).
kubectl get --no-headers persistentvolumes|awk '$5=="Released" { print $1 }'|xargs echo "kubectl delete persistentvolumes"

- 121
- 3
Until you keep pvc your pv will be in Bound state. So you can just go and delete unused pvc with:
kubectl -n namespace get pvc -o name | grep myname | xargs kubectl -n namespace delete

- 2,552
- 1
- 15
- 24
-
1do I understand correctly you suggesting to delete all pvc? It's same as ```kubectl delete pvc,pv --all -n namespace``` ? – Pav K. Nov 08 '18 at 09:12
Yeah, first you need to delete unused PVC.
With kubectl get pvc --all-namespaces
you can list all of them in all namespaces along with the corresponding PVs.
In order to delete unused PVs you need to change its ReclaimPolicy because if it's set to Retain the PVs won't be deleted but will hang in "Released" status. So in order to do that you need to patch PV (it's not possible to edit it manually with kubectl edit
for some reason):
kubectl patch pv <your-pv-name> -p '{"spec":{"persistentVolumeReclaimPolicy":"Delete"}}'

- 1,260
- 9
- 9