Sub topic 9
Reclaim policies: Retain and Delete
This chapter assumes you can apply YAML with kubectl and that you know how a PersistentVolumeClaim (PVC) binds to a PersistentVolume (PV). Here you will see what happens to the PV and its data when the claim goes away.
What a reclaim policy does
A reclaim policy is a field on a PV, persistentVolumeReclaimPolicy. It tells the cluster what to do with the volume after the PVC bound to it is deleted. Two values matter in practice:
Retainkeeps the PV and the data on it.Deleteremoves the PV object and also the storage asset behind it, such as a cloud disk.
A third value, Recycle, is deprecated. It ran a basic rm -rf on the volume. Use dynamic provisioning instead.
| Policy | PV object after PVC deletion | Underlying data | Typical use |
|---|---|---|---|
| Retain | Stays, phase `Released` | Kept | Important data, manual recovery |
| Delete | Removed | Removed | Disposable or easily rebuilt data |
Where the default comes from
A statically created PV defaults to Retain. A dynamically provisioned PV copies the reclaimPolicy of its StorageClass, and that field defaults to Delete. The fast class from the StorageClass chapter sets reclaimPolicy: Delete explicitly, so every volume it creates is destroyed with its claim.
Retain in practice
The demo PV in this tutorial sets persistentVolumeReclaimPolicy: Retain. Delete the claim and check the volume:
kubectl delete pvc demo-pvc
kubectl get pv demo-pvThe STATUS column shows Released. The data under /mnt/data is still there. The PV is not available to new claims, because the old claim's reference is still recorded in spec.claimRef.
To reuse the volume, an administrator does this by hand:
- Copy or clean the data, depending on whether the next user should see it.
- Remove the stale claim reference.
- Confirm the PV shows
Available.
kubectl patch pv demo-pv -p '{"spec":{"claimRef":null}}'Alternatively, delete the PV object and create a new one that points at the same storage. With a cloud disk, deleting the PV object leaves the disk itself in place under Retain, so you must remove the disk yourself when you no longer need it.
Delete in practice
With Delete, removing the PVC triggers removal of the PV and then of the backing storage through the CSI driver. No manual step is needed, and no recovery is possible unless you have a snapshot or backup. This suits scratch data and development clusters. It is risky for databases, because deleting a namespace or a Helm release can delete every claim in it.
Changing the policy on a live volume
You can change the policy on an existing PV at any time. Do this before you delete a claim whose data you want to keep:
kubectl patch pv <pv-name> -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'Find the PV name with kubectl get pvc, which lists it in the VOLUME column.
Exercise
You delete a PVC whose PV uses the Retain policy. The PV now shows Released, but a new PVC that matches it stays Pending. Why, and what must you do?
Show answer
A Released PV still carries the old claim in spec.claimRef, so it is not Available for binding. After you deal with the old data, clear the reference (for example with kubectl patch pv <name> -p '{"spec":{"claimRef":null}}') so the PV becomes Available again.