Search code examples
kuberneteskubernetes-pvc

Why do PVCs persist when using 'kubectl delete ns' while Pods are deleted, and the entire namespace is removed?


I've noticed that when I use the kubectl delete ns command to remove a namespace in Kubernetes, it successfully deletes Pods and all other resources within the namespace, but the Persistent Volume Claims (PVCs) seem to persist. Even more oddly, the PVCs disappear along with the namespace.

Is this expected behavior, and can someone explain why PVCs remain but PVCs also disappear when deleting a namespace? Is there a way to automatically delete both PVCs and their associated Persistent Volumes (PVs) when deleting a namespace?

I would appreciate any insights and best practices for managing PVCs and PVs when deleting a namespace in Kubernetes.


Solution

  • Yes! This is how things should behave even I have observed the same during the time when I was learning Kubernetes.

    Reference for PV- Persistent Volume Kubernetes Official document

    Persistent Volume - Is a cluster-level resource that represents a piece of networked storage in the cluster. PVs are used to provide storage resources to pods running within the cluster. This implies that PV is cluster level resource i.e. if you run the command kubectl delete ns dev Persistent Volume will not be deleted only things like service, deployment, Persistent Volume claim will be removed. Please go through the Kubernetes document for more information.

    Persistent Volume Claim - Is a namespace level resource that represents a request for storage by a pod.

    There is a simple way to test this create a PV,PVC and a pod. Ensure to create PVC and pods in a namespace once all resources are bound and running, delete the namespace and observe what happens.

    Create a test namespace and then PV,PVC and pods was created

    Now delete the namespace test kubectl delete ns test

    Post running delete ns command

    You will observe that PVC and pod gets deleted but the PV remains, this is due to fact that PV is a cluster level resource hence it is expected to retain even if that namespace is deleted.