Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save korutx/fbf64abea89df6e3f820f2cbff6c39a4 to your computer and use it in GitHub Desktop.
Save korutx/fbf64abea89df6e3f820f2cbff6c39a4 to your computer and use it in GitHub Desktop.

Change Persistent Volume Reclaim Policy to Retain

This gist demonstrates how to change the reclaim policy of a Persistent Volume (PV) in Kubernetes from "Delete" to "Retain".

Prerequisites

  • kubectl configured to your cluster
  • Proper permissions to modify PV resources

Commands

  1. List all PVCs in a specific namespace:

    kubectl get pvc -n <namespace>

    Replace <namespace> with your actual namespace (e.g., monitoring).

  2. Find the PVC name you're interested in from the output.

  3. Get the PV name associated with the PVC:

    kubectl get pvc <pvc-name> -n <namespace> -o jsonpath='{.spec.volumeName}'

    Replace <pvc-name> with your PVC name and <namespace> with the correct namespace.

  4. Once you have the PV name, use this command to change its reclaim policy:

    kubectl patch pv <pv-name> -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'

Purpose

Changing the reclaim policy to "Retain" ensures that the PV and its data are not automatically deleted when the associated PersistentVolumeClaim (PVC) is deleted. This is useful for preserving data or when you want to manually manage the lifecycle of the volume.

Verification

After running the command, verify the change:

kubectl get pv <pv-name> -o jsonpath='{.spec.persistentVolumeReclaimPolicy}'

Replace <pv-name> with your actual PV name.

This should output: Retain

Note

Be cautious when changing reclaim policies. "Retain" policy means you'll need to manually reclaim the storage resource when it's no longer needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment