Bird
Raised Fist0
Kubernetesdevops~5 mins

Reclaim policies (Retain, Delete) in Kubernetes - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you delete a Kubernetes PersistentVolumeClaim, the reclaim policy decides what happens to the actual storage. It can either keep the storage for manual cleanup or delete it automatically to free space.
When you want to keep data safe even after deleting the claim, for backup or manual recovery.
When you want storage to be cleaned up automatically to avoid leftover unused volumes.
When managing storage lifecycle in a shared cluster with multiple users.
When setting up persistent storage for stateful applications like databases.
When automating cleanup of storage resources in development or testing environments.
Config File - persistent-volume.yaml
persistent-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /mnt/data
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv-delete
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  hostPath:
    path: /mnt/data-delete

This file defines two PersistentVolumes (PVs). Each PV has a persistentVolumeReclaimPolicy key:

  • Retain: Keeps the storage after the claim is deleted, so data is safe but manual cleanup is needed.
  • Delete: Automatically deletes the storage when the claim is deleted, freeing space immediately.

The hostPath simulates local storage on the node for simplicity.

Commands
Create two PersistentVolumes with different reclaim policies: one retains storage, the other deletes it automatically.
Terminal
kubectl apply -f persistent-volume.yaml
Expected OutputExpected
persistentvolume/example-pv created persistentvolume/example-pv-delete created
Check the status and reclaim policies of the created PersistentVolumes to confirm they are set correctly.
Terminal
kubectl get pv example-pv example-pv-delete -o wide
Expected OutputExpected
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE example-pv 1Gi RWO Retain Available <none> 1m example-pv-delete 1Gi RWO Delete Available <none> 1m
→
-o wide - Shows detailed information including reclaim policy
Delete the PersistentVolumeClaim to trigger the reclaim policy on the associated PersistentVolume.
Terminal
kubectl delete pvc example-pvc
Expected OutputExpected
persistentvolumeclaim "example-pvc" deleted
Check the PersistentVolumes after deleting the claim to see if storage is retained or deleted based on the reclaim policy.
Terminal
kubectl get pv example-pv example-pv-delete
Expected OutputExpected
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM example-pv 1Gi RWO Retain Released example-pv-delete 1Gi RWO Delete Available
Key Concept

If you remember nothing else from this pattern, remember: the reclaim policy controls whether storage is kept or deleted when the claim is removed.

Common Mistakes
Setting reclaim policy to Delete but expecting data to remain after claim deletion.
Delete policy removes the storage automatically, so data is lost.
Use Retain policy if you want to keep data after deleting the claim.
Not checking the reclaim policy before deleting PersistentVolumeClaims.
You might accidentally delete important data or leave unused storage behind.
Always verify reclaim policy with 'kubectl get pv' before deleting claims.
Assuming reclaim policy applies to PersistentVolumeClaims instead of PersistentVolumes.
Reclaim policy is set on PersistentVolumes, not claims, so setting it on claims has no effect.
Set reclaim policy in the PersistentVolume spec.
Summary
Create PersistentVolumes with reclaim policies Retain or Delete to control storage cleanup.
Use 'kubectl get pv' to verify reclaim policies and volume status.
Deleting a PersistentVolumeClaim triggers the reclaim policy on its PersistentVolume.

Practice

(1/5)
1. What does the Retain reclaim policy do in Kubernetes Persistent Volumes?
easy
A. Keeps the volume and data after the PersistentVolumeClaim is deleted for manual cleanup.
B. Automatically deletes the volume and data when the PersistentVolumeClaim is deleted.
C. Prevents any deletion of the PersistentVolumeClaim.
D. Creates a backup of the volume before deletion.

Solution

  1. Step 1: Understand reclaim policies

    Reclaim policies define what happens to storage after the claim is deleted.
  2. Step 2: Identify Retain behavior

    The Retain policy keeps the volume and data intact for manual intervention.
  3. Final Answer:

    Keeps the volume and data after the PersistentVolumeClaim is deleted for manual cleanup. -> Option A
  4. Quick Check:

    Retain = Keep data safe [OK]
Hint: Retain means keep data after claim deletion [OK]
Common Mistakes:
  • Confusing Retain with automatic deletion
  • Thinking Retain backs up data automatically
  • Assuming Retain prevents claim deletion
2. Which of the following is the correct YAML snippet to set a PersistentVolume reclaim policy to Delete?
easy
A. persistentVolumeReclaimPolicy: Retain
B. persistentVolumeReclaimPolicy: Delete
C. persistent_volume_reclaim_policy: Delete
D. persistentVolumeReclaimPolicy: delete

Solution

  1. Step 1: Check correct YAML key and value

    The key is persistentVolumeReclaimPolicy, and value is case-sensitive.
  2. Step 2: Identify correct value for Delete

    The value must be exactly Delete with capital D.
  3. Final Answer:

    persistentVolumeReclaimPolicy: Delete -> Option B
  4. Quick Check:

    Correct key and capitalized value = A [OK]
Hint: Use exact key 'persistentVolumeReclaimPolicy' and capitalized 'Delete' [OK]
Common Mistakes:
  • Using lowercase 'delete' instead of 'Delete'
  • Using underscore instead of camelCase key
  • Mixing Retain and Delete values
3. Given this PersistentVolume YAML snippet:
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-example
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /data/pv-example

What happens to the volume when the PersistentVolumeClaim using this PV is deleted?
medium
A. The volume and data are deleted automatically.
B. The volume is unbound but data is deleted.
C. The volume is retained and data remains for manual cleanup.
D. The volume is backed up before deletion.

Solution

  1. Step 1: Identify reclaim policy in YAML

    The reclaim policy is set as Retain in persistentVolumeReclaimPolicy.
  2. Step 2: Understand Retain effect on deletion

    Retain means the volume and data stay after claim deletion for manual handling.
  3. Final Answer:

    The volume is retained and data remains for manual cleanup. -> Option C
  4. Quick Check:

    Retain policy = keep volume and data [OK]
Hint: Retain policy keeps volume after claim deletion [OK]
Common Mistakes:
  • Assuming data deletes automatically with Retain
  • Confusing persistentVolumeReclaimPolicy with reclaimPolicy key
  • Thinking volume is backed up automatically
4. You set a PersistentVolume reclaim policy to Delete but after deleting the PersistentVolumeClaim, the volume still exists. What is the most likely cause?
medium
A. The reclaim policy was misspelled in the YAML and ignored.
B. The volume was manually retained by an admin.
C. The PersistentVolumeClaim was not actually deleted.
D. The storage class does not support automatic deletion.

Solution

  1. Step 1: Understand Delete reclaim policy

    Delete policy should remove the volume automatically after claim deletion.
  2. Step 2: Check storage class support

    If the storage class does not support dynamic deletion, volume remains despite policy.
  3. Final Answer:

    The storage class does not support automatic deletion. -> Option D
  4. Quick Check:

    Storage class support needed for Delete policy [OK]
Hint: Delete needs storage class support to remove volume [OK]
Common Mistakes:
  • Assuming reclaim policy misspelling always causes failure
  • Ignoring storage class role in deletion
  • Not verifying claim deletion status
5. You want to ensure that when a PersistentVolumeClaim is deleted, the underlying storage is kept intact for backup purposes, but you also want to automate cleanup later. Which reclaim policy should you use and what additional step is needed?
hard
A. Use Retain policy and configure a cleanup job to delete volumes later.
B. Use Delete policy and rely on automatic cleanup only.
C. Use Retain policy and manually delete the volume after backup.
D. Use Delete policy and create snapshots before deletion.

Solution

  1. Step 1: Choose policy to keep data after claim deletion

    Retain policy keeps the volume and data safe after claim deletion.
  2. Step 2: Automate cleanup after backup

    Since Retain does not delete automatically, a cleanup job is needed to remove volumes later.
  3. Final Answer:

    Use Retain policy and configure a cleanup job to delete volumes later. -> Option A
  4. Quick Check:

    Retain + cleanup job = safe backup + automation [OK]
Hint: Retain keeps data; add cleanup job for automation [OK]
Common Mistakes:
  • Assuming Delete keeps data for backup
  • Expecting Retain to delete volumes automatically
  • Not planning manual or automated cleanup after Retain
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard