2017-01-06 09:56:10 +00:00
---
2017-06-08 22:13:29 +00:00
title: Change the Reclaim Policy of a PersistentVolume
2020-05-30 19:10:23 +00:00
content_type: task
2017-01-06 09:56:10 +00:00
---
2020-05-30 19:10:23 +00:00
<!-- overview -->
2017-01-06 09:56:10 +00:00
This page shows how to change the reclaim policy of a Kubernetes
PersistentVolume.
2020-05-30 19:10:23 +00:00
## {{% heading "prerequisites" %}}
2017-01-06 09:56:10 +00:00
2018-05-05 16:00:51 +00:00
{{< include " task-tutorial-prereqs . md " > }} {{< version-check > }}
2017-01-06 09:56:10 +00:00
2020-05-30 19:10:23 +00:00
<!-- steps -->
2017-01-06 09:56:10 +00:00
2017-01-18 18:18:37 +00:00
## Why change reclaim policy of a PersistentVolume
2017-01-06 09:56:10 +00:00
2020-07-22 21:38:48 +00:00
PersistentVolumes can have various reclaim policies, including "Retain",
"Recycle", and "Delete". For dynamically provisioned PersistentVolumes,
2017-01-06 09:56:10 +00:00
the default reclaim policy is "Delete". This means that a dynamically provisioned
volume is automatically deleted when a user deletes the corresponding
2020-07-22 21:38:48 +00:00
PersistentVolumeClaim. This automatic behavior might be inappropriate if the volume
2017-01-06 09:56:10 +00:00
contains precious data. In that case, it is more appropriate to use the "Retain"
2020-07-22 21:38:48 +00:00
policy. With the "Retain" policy, if a user deletes a PersistentVolumeClaim,
2021-04-01 09:52:56 +00:00
the corresponding PersistentVolume will not be deleted. Instead, it is moved to the
2020-07-22 21:38:48 +00:00
Released phase, where all of its data can be manually recovered.
2017-01-06 09:56:10 +00:00
2017-01-18 18:18:37 +00:00
## Changing the reclaim policy of a PersistentVolume
2017-01-06 09:56:10 +00:00
1. List the PersistentVolumes in your cluster:
2018-10-30 16:44:40 +00:00
```shell
kubectl get pv
```
2017-01-06 09:56:10 +00:00
The output is similar to this:
2018-09-06 06:44:16 +00:00
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-b6efd8da-b7b5-11e6-9d58-0ed433a7dd94 4Gi RWO Delete Bound default/claim1 manual 10s
pvc-b95650f8-b7b5-11e6-9d58-0ed433a7dd94 4Gi RWO Delete Bound default/claim2 manual 6s
pvc-bb3ca71d-b7b5-11e6-9d58-0ed433a7dd94 4Gi RWO Delete Bound default/claim3 manual 3s
2017-01-06 09:56:10 +00:00
2020-02-08 19:23:53 +00:00
This list also includes the name of the claims that are bound to each volume
2017-01-06 09:56:10 +00:00
for easier identification of dynamically provisioned volumes.
2017-08-21 01:25:46 +00:00
1. Choose one of your PersistentVolumes and change its reclaim policy:
2017-01-06 09:56:10 +00:00
2018-10-30 16:44:40 +00:00
```shell
kubectl patch pv < your-pv-name > -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
```
2017-01-06 09:56:10 +00:00
where `<your-pv-name>` is the name of your chosen PersistentVolume.
2020-02-08 19:23:53 +00:00
{{< note > }}
On Windows, you must _double_ quote any JSONPath template that contains spaces (not single quote as shown above for bash). This in turn means that you must use a single quote or escaped double quote around any literals in the template. For example:
```cmd
kubectl patch pv < your-pv-name > -p "{\"spec\":{\"persistentVolumeReclaimPolicy\":\"Retain\"}}"
```
{{< / note > }}
2017-01-06 09:56:10 +00:00
1. Verify that your chosen PersistentVolume has the right policy:
2018-10-30 16:44:40 +00:00
```shell
kubectl get pv
```
2017-01-06 09:56:10 +00:00
The output is similar to this:
2018-09-06 06:44:16 +00:00
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-b6efd8da-b7b5-11e6-9d58-0ed433a7dd94 4Gi RWO Delete Bound default/claim1 manual 40s
pvc-b95650f8-b7b5-11e6-9d58-0ed433a7dd94 4Gi RWO Delete Bound default/claim2 manual 36s
pvc-bb3ca71d-b7b5-11e6-9d58-0ed433a7dd94 4Gi RWO Retain Bound default/claim3 manual 33s
2017-01-06 09:56:10 +00:00
In the preceding output, you can see that the volume bound to claim
`default/claim3` has reclaim policy `Retain` . It will not be automatically
deleted when a user deletes claim `default/claim3` .
2020-05-30 19:10:23 +00:00
## {{% heading "whatsnext" %}}
2017-04-19 17:56:47 +00:00
* Learn more about [PersistentVolumes ](/docs/concepts/storage/persistent-volumes/ ).
2017-09-24 18:41:34 +00:00
* Learn more about [PersistentVolumeClaims ](/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims ).
2017-01-06 09:56:10 +00:00
2021-10-12 20:36:37 +00:00
### References {#reference}
2017-01-06 09:56:10 +00:00
2021-10-12 20:36:37 +00:00
* {{< api-reference page = "config-and-storage-resources/persistent-volume-v1" > }}
* Pay attention to the `.spec.persistentVolumeReclaimPolicy` [field ](https://kubernetes.io/docs/reference/kubernetes-api/config-and-storage-resources/persistent-volume-v1/#PersistentVolumeSpec ) of PersistentVolume.
* {{< api-reference page = "config-and-storage-resources/persistent-volume-claim-v1" > }}
2020-05-30 19:10:23 +00:00
2018-05-05 16:00:51 +00:00
2017-01-06 09:56:10 +00:00