Deleting a Persistent Volume

This topic describes how to delete a PV, or retain a PV after all associated PVCs are deleted. To delete PVCs, see Deleting a Persistent Volume Claim.

For file system storage, the default behavior is to retain the PV when all associated PVCs are deleted.

For block volume storage, the default behavior is to delete the PV when all associated PVCs are deleted. You might prefer to retain the PV after all associated PVCs are deleted, for example if the volume contains critical data. See Retaining a Persistent Volume.

If a PV is retained, you can optionally delete the PV later.

Deleting a Persistent Volume Claim

To delete a PVC, first delete all pods that are using that PVC. If you attempt to delete the PVC while a pod is still using the PVC, the PVC will be stuck in Terminating state and will not be deleted. When all the pods that are using that PVC are deleted, the PVC will be deleted.

  1. List all pods that are using the PVC.

    Ensure that you have JQ command line utilities installed to query JSON objects.

    Use the following command to list pods across all the namespaces that are associated with the PVC that you want to delete.

    $ kubectl get pods --all-namespaces -o=json | jq -c '.items[] | {name: .metadata.name, namespace: .metadata.namespace, claimName: .spec | select(has("volumes")).volumes[] | select(has("persistentVolumeClaim")).persistentVolumeClaim.claimName} | select(.claimName != null)'
    
    {"name":"pod1_name","namespace":"namespace1_name","claimName":"claim1_name"}
    {"name":"pod2_name","namespace":"namespace1_name","claimName":"claim1_name"}
    {"name":"pod3_name","namespace":"namespace2_name","claimName":"claim2_name"}

    To list pods only in the current namespace, use the same command as the preceding command except omit the --all-namespaces option.

  2. Delete all pods that are using the PVC.

    Use the pod names reported by the kubectl get pods command that are associated with the claimName that you want to delete.

    $ kubectl delete pod pod1_name pod2_name
  3. Delete the PVC.

    $ kubectl delete pvc claim1_name
  4. (Optional) Delete the PV.

    If the Persistent Volume Reclaim Policy is Delete, the PV is automatically deleted when all PVCs that are associated with this PV are deleted.

    To list all PVCs, use the kubectl get pvc command.

    If the Persistent Volume Reclaim Policy is Retain, you can use the following command to delete the PV:

    $ kubectl delete pv pv_name

Retaining a Persistent Volume

Rather than delete a PV, you might prefer to retain the PV after all associated PVCs are deleted, for example if the volume contains critical data. See Changing the Reclaim Policy of a Persistent Volume for instructions to change the reclaim policy of the PV so that the PV will be retained after all associated PVCs are deleted.

If the Persistent Volume Reclaim Policy is Delete, the PV is automatically deleted when all PVCs that are associated with this PV are deleted. To prevent this behavior, specify the Retain policy. With the Retain policy, the PV is not deleted but is released of its claim. See Recovering the Data from a Released Persistent Volume for instructions to recover the data.

If you decide you want to delete the PV even though it was retained, or you want to delete the PV after you have recovered the data, use the following command:

$ kubectl delete pv pv_name

Changing the Reclaim Policy of a Persistent Volume

  1. List the PVs in the cluster.

    $ kubectl get pv
    NAME    CAPACITY  ACCESS MODES  RECLAIM POLICY  STATUS  CLAIM            STORAGECLASS  REASON  AGE
    fss-pv  200Gi     RWX           Delete          Bound   default/fss-pvc  pca-fss               20h
  2. Change the reclaim policy of the PV.

    $ kubectl patch pv fss-pv -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
  3. Verify the reclaim policy change.

    The RECLAIM POLICY column should now say Retain.

    $ kubectl get pv

Recovering the Data from a Released Persistent Volume

The PV is not available for another claim after the PV has been released of its previous claim because the previous claimant's data is still on the volume. Recover the data and then re-create the PV using the same storage to make a new claim on that storage.

  1. Delete the PV.

    $ kubectl delete pv pv_name

    The associated block volume or file system still exists after the PV is deleted.

  2. Manually recover and clean up the data on the block volume or file system.

  3. (Optional) Manually delete the block volume or file system.

    To reuse the same block volume or file system, create a new PV with the same storage asset definition.