How to delete only unmounted PVCs and PVs?
Asked Answered
O

6

18

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?

Outdoor answered 8/11, 2018 at 6:50 Comment(0)
O
27

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
Outdoor answered 4/12, 2019 at 14:24 Comment(0)
R
22

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 removes Name: and Namespace: since they just get in the way
  • paste puts the Name of the PVC and it's Namespace on the same line
  • xargs -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!

Retrogressive answered 15/1, 2020 at 20:14 Comment(1)
super 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!Humberto
A
2

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 {}
Acephalous answered 8/11, 2018 at 19:32 Comment(5)
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 -> deleteOutdoor
kubectl get pvc -o name only returns name. it doesn't return details. you probably meant -o wide.Outdoor
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.Acephalous
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 templatesOutdoor
You can probably do that with -o=jsonpath. Can you post a sample for mounted and unmounted?Acephalous
P
2

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"
Poaceous answered 4/3, 2020 at 13:13 Comment(0)
P
1

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

Plaided answered 8/11, 2018 at 8:2 Comment(1)
do I understand correctly you suggesting to delete all pvc? It's same as kubectl delete pvc,pv --all -n namespace ?Outdoor
C
0

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"}}'
Craggie answered 14/11, 2018 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.