how to property delete PersistentVolume in kubernetes

2/22/2020

I am using this command to delete PersistentVolume:

  ~/Library/Mobile Documents/com~apple~CloudDocs/Document/source/config/k8s/redis ⌚ 12:23:38
$ kubectl delete -f pv.yaml
persistentvolume "nfs-pv1" deleted
persistentvolume "nfs-vp2" deleted
persistentvolume "nfs-pv3" deleted
persistentvolume "nfs-pv4" deleted
persistentvolume "nfs-pv5" deleted
persistentvolume "nfs-pv6" deleted

but the progress is stuck.How to smoothly remove the persistentvolume?I check the kubernetes dashboard(kubernetes version v1.15.2) and found nfs-pv5 volume still remain.I have tried to tweak the service using persistentvolume scale 0(stopping all pods using persistentvolume) .

-- Dolphin
kubernetes

1 Answer

2/22/2020

If a user deletes a PVC in active use by a Pod, the PVC is not removed immediately. PVC removal is postponed until the PVC is no longer actively used by any Pods. Also, if an admin deletes a PV that is bound to a PVC, the PV is not removed immediately. PV removal is postponed until the PV is no longer bound to a PVC.

You can see that a PVC is protected when the PVC’s status is Terminating and the Finalizers list includes kubernetes.io/pvc-protection.

So you need to delete the pod first and then delete the PVC and then delete PV or you can edit the pv and pvc to remove the finalizer.

kubectl patch pvc PVC_NAME -p '{"metadata":{"finalizers": []}}' --type=merge
kubectl patch pvc PV_NAME -p '{"metadata":{"finalizers": []}}' --type=merge
-- Arghya Sadhu
Source: StackOverflow