How to delete only unmounted PVCs and PVs?

11/8/2018

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?

-- Pav K.
kubernetes
kubernetes-pvc

6 Answers

3/4/2020

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"
-- Maximilien D
Source: StackOverflow

11/14/2018

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"}}'
-- Anna Slastnikova
Source: StackOverflow

12/4/2019

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
-- Pav K.
Source: StackOverflow

1/15/2020

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:.*

quot;

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:.*
quot;
| grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*
quot;
| 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!

-- iridian
Source: StackOverflow

11/8/2018

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 {}
-- Rico
Source: StackOverflow

11/8/2018

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

-- abinet
Source: StackOverflow