How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
The deletion of PV(Persistent Volume) and PVC(Persistent Volume claim) is pretty much dependent on the Delete reclaim policy. When you are planning to delete the Persistent Volume as well as Persistent Volume Claim then you must follow an order -
- First delete - Persistent Volume Claim
- Second delete- Persistent Volume
(Note* - You should never delete PV(Persistent Volume) without deleting its PVC(Persistent Volume Claim))
In this blog we will address on -
- How to delete PV(Persistent Volume), PVC(Persistent Volume Claim)?
- How to delete PV(Persistent Volume), PVC(Persistent Volume Claim) stuck in terminating state?
- How to delete Storage Class and PVC(Persistent Volume claim) associated with it?
Before you start targeting PV and PVC for deletion you must know the Delete Reclaim Policy of Kubernetes -
Delete Reclaim Policy- There are two types of volume provisioning in Kubernetes Static and Dynamic.
- Static Volume Provisioning - You have to manually configure the PV, PVC but when you need to delete both PV, PVC then you must start from PVC and then go for PV.
- Dynamic Volume Provisioning - You have to set up Storage Class along with PVC and Storage Class will take of dynamic provisioning of PV(Persistent Volume). If you delete the PVC(Persistent Volume Claim) then all the PV(Persistent Volume) provisioned dynamically will be deleted.
1. How to delete PV(Persistent Volume), PVC(Persistent Volume Claim)?
Let's take an example to understand how PV(Persistent Volume), PVC(Persistent Volume Claim) are associated with each other and how to delete both one by one.
Here are my PV(Persistent Volume) and PVC(Persistent Volume Claim) present inside my Kubernetes cluster -
1. PV(Persistent Volume) - jhooq-pv
2. PVC(Persistent Volume Claim) - jhooq-pv-claim
To delete both I need to start from PVC -
1kubectl delete pvc jhooq-pv-claim
2persistentvolumeclaim "jhooq-pv-claim" deleted
Now Delete PV -
1kubectl delete pv jhooq-pv
2persistentvolume "jhooq-pv" deleted
But instead, if you will try to delete the jhooq-pv before deleting the jhooq-pv-claim it will get stuck in the terminating state -
In the next step, we will see how to delete the Persistent Volume and Persistent Volume Claim stuck in the terminating state.
2. How to delete PV(Persistent Volume), PVC(Persistent Volume Claim) stuck in terminating state?
If you are looking for the solution to delete the PV(Persistent Volume), PVC(Persistent Volume Claim) which are stuck in the terminating state then you have to pay careful attention to the Kubernetes Finalizer.
Each Kubernetes resource running in the cluster has Finalizers associated with it.
- Finalizers prevent accidental deletion of resources(Persistent Volume, Persistent Volume Claim, POD, etc..)
- If you accidentally issue kubectl delete command on Kubernetes resource and if there is a finalizer associated with that resource then it is going to put the resource in Read-Only mode and prevent it from deletion.
So if your Kubernetes resource such as Persistent Volume, Persistent Volume Claim, or POD is stuck in the terminating state then you must remove the Finalizer from that resource and after removing the Finalizer you should be able to delete the resource.
2.1 How to remove the Finalizer from Persistent Volume, Persistent Volume Claim or POD
To remove the Finalizer you have to use the kubectl patch command with the exact resource name.
Here is my Persistent Volume with the name jhooq-pv and the following kubectl patch command will be used for removing the finalizer -
1kubectl patch pv jhooq-pv -p '{"metadata":{"finalizers":null}}'
Similarly, if your Persistent Volume Claim is stuck in Terminating state then run the following kubectl patch command to remove the Finalizer -
1kubectl patch pvc jhooq-pv-claim -p '{"metadata":{"finalizers":null}}'
Now after patching both Persistent Volume Claim and Persistent Volume, you can delete both the resources.
Start deleting the Persistent Volume Claim first -
1kubectl delete pvc jhooq-pv-claim
2persistentvolumeclaim "jhooq-pv-claim" deleted
After successful deletion of Persistent Volume, claim delete the Persistent Volume -
1kubectl delete pvc jhooq-pv-claim
2persistentvolumeclaim "jhooq-pv-claim" deleted
2.2 Use the force delete
Also, try to force delete both Persistent Volume claim and Persistent Volume -
1kubectl delete pvc --grace-period=0 --force --namespace mynamespace jhooq-pv-claim
1kubectl delete pv --grace-period=0 --force --namespace mynamespace jhooq-pv
3. How to delete Storage Class and PVC(Persistent Volume claim) associated with it?
In the previous Section-1, Section-2 we have seen how to delete the static Persistent Volume and Persistent Volume claim. But let's take an example where you need to delete dynamic volumes which are provisioned using Storage classes.
My storage class is - jhooq-pv-storage-class
Use the following command -
1kubectl delete storageclass jhooq-pv-storage-class
But in-case if your storage class is stuck in the terminating state then you should remove the finalizer from the storage class then proceed with the deletion -
1kubectl patch storageclass jhooq-pv-storage-class -p '{"metadata":{"finalizers":null}}'
Also, try to force delete the Storage class -
1kubectl delete storageclass --grace-period=0 --force --namespace mynamespace jhooq-pv-storage-class
Learn more On Kubernetes -
- Setup kubernetes on Ubuntu
- Setup Kubernetes on CentOs
- Setup HA Kubernetes Cluster with Kubespray
- Setup HA Kubernetes with Minikube
- Setup Kubernetes Dashboard for local kubernetes cluster
- Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
- How to use Persistent Volume and Persistent Volume Claims in Kubernetes
- Deploy Spring Boot Microservice on local Kubernetes cluster
- Deploy Spring Boot Microservice on Cloud Platform(GCP)
- Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
- How to setup kubernetes jenkins pipeline on AWS?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- How to fix kubernetes pods getting recreated?
- How to delete all kubernetes PODS?
- How to use Kubernetes secrets?
- Share kubernetes secrets between namespaces?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Delete Kubernetes POD stuck in terminating state?
Posts in this Series
- Kubernetes Cheat Sheet for day to day DevOps operations?
- Delete Kubernetes POD stuck in terminating state?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Share kubernetes secrets between namespaces?
- How to use Kubernetes secrets?
- How to delete all kubernetes PODS?
- kubernetes pods getting recreated?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- kubectl export yaml OR How to generate YAML for deployed kubernetes resources
- Kubernetes Updates
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- Kubernetes cluster setup with Jenkins
- How to use Persistent Volume and Persistent Claims | Kubernetes
- How to fix ProvisioningFailed persistentvolume controller no volume plugin matched
- Fixing – Cannot bind to requested volume: storageClasseName does not match
- Fixing – pod has unbound immediate persistentvolumeclaims or cannot bind to requested volume incompatible accessmode
- How to fix kubernetes dashboard forbidden 403 error – message services https kubernetes-dashboard is forbidden User
- How to fix Kubernetes – error execution phase preflight [preflight]
- Deploy Spring Boot microservices on kubernetes?
- How to fix – ansible_memtotal_mb minimal_master_memory_mb
- How to use kubespray – 12 Steps for Installing a Production Ready Kubernetes Cluster
- How to setup kubernetes on CentOS 8 and CentOS 7
- How to fix – How to fix - ERROR Swap running with swap on is not supported. Please disable swap
- 14 Steps to Install kubernetes on Ubuntu 20.04(bento/ubuntu-20.04), 18.04(hashicorp/bionic64)
- Kubernetes Dashboard | Kubernetes Admin GUI | Kubernetes Desktop Client
- Install Kubernetes with Minikube