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 -

  1. First delete - Persistent Volume Claim
  2. 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 -

  1. How to delete PV(Persistent Volume), PVC(Persistent Volume Claim)?
  2. How to delete PV(Persistent Volume), PVC(Persistent Volume Claim) stuck in terminating state?
  3. 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.

  1. 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.
  2. 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 -

Kubernetes Persistent Volume got stuck in 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.

  1. Finalizers prevent accidental deletion of resources(Persistent Volume, Persistent Volume Claim, POD, etc..)
  2. 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 -

  1. Setup kubernetes on Ubuntu
  2. Setup Kubernetes on CentOs
  3. Setup HA Kubernetes Cluster with Kubespray
  4. Setup HA Kubernetes with Minikube
  5. Setup Kubernetes Dashboard for local kubernetes cluster
  6. Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
  7. How to use Persistent Volume and Persistent Volume Claims in Kubernetes
  8. Deploy Spring Boot Microservice on local Kubernetes cluster
  9. Deploy Spring Boot Microservice on Cloud Platform(GCP)
  10. Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
  11. CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
  12. kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
  13. How to setup kubernetes jenkins pipeline on AWS?
  14. Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
  15. How to fix kubernetes pods getting recreated?
  16. How to delete all kubernetes PODS?
  17. How to use Kubernetes secrets?
  18. Share kubernetes secrets between namespaces?
  19. How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
  20. Delete Kubernetes POD stuck in terminating state?

Posts in this Series