Fix accidentaly deleted pvc.
How to claim a pre-existing persistent Volume with a new pvc
If the persistent volume claim was accidentally deleted, and the persistent volume is still present, you can restore the PVC and PV by performing the following actions:
- Make sure there is no persistent volume claim (pvc) present for this volume (pv) (e.g. if you provisoned a new one with the same name, resulting in a new empty persistent volume)
- Modify the persistent volume claim reference
- Create a new PVC, with volumeName pointing to the PV
Note that for this to be possible, your persisent volume needs to be configured with a reclaim policy
If it’s configured with
Delete, your persistent volume will be deleted and you need to restore from a back-up.
Validate the pv
check that the persistent volume has the status
Released. Do this by using:
$ kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-1d6cb933-2706-4471-9a89-e895a9cf3439 4Gi RWO Retain Released default/test-database gp2-immediate 50d
If it’s not released but for example
Bound, a pvc is still present for this volume.
You will note that
CLAIM still has the previous PVC name present.
Update the claim ref
Edit the persistent volume, which is marked as
Released. If the Persistent Volume is marked as Available, no other action needs to be taken. If it is marked as any other value, do not modify the claimRef. You may need to perform step 1 first.
$ kubectl edit pv <persistent-volume>;
Edit this block from the persistent volume that you want to link to the new persistent volume claim:
claimRef: apiVersion: v1 kind: PersistentVolumeClaim name: test-database namespace: default resourceVersion: "68569655" uid: f4a2f6c4-316c-4b7a-952c-c7f51a8ec354
- Remove the fields
- Update the
namespaceif necessary. These refer to the new pvc you will be creating. Usually these will be the same as before.
Claim the persistent volume
To claim the persistent volume, create a new PVC, which references the persistent volume. Note that volumeName is the name of the persistent volume in the example below.
If this PVC is part of a chart install, you may need to include the meta.helm.sh labels and annotations as well. They are present in the example, but needn’t be present if the original PVC was not installed or managed with helm.
Create the PVC:
apiVersion: v1 kind: PersistentVolumeClaim metadata: annotations: meta.helm.sh/release-name: test meta.helm.sh/release-namespace: default labels: app.kubernetes.io/managed-by: Helm name: test-database namespace: default spec: accessModes: - ReadWriteMany resources: requests: storage: 4Gi storageClassName: gp2-immediate volumeMode: Filesystem volumeName: d6cb933-2706-4471-9a89-e895a9cf3439
To validate the actions have been performed correctly, you query the available persistent volumes. It should now should the PV marked as
Bound. The PVC should display the correct PV name.
kubectl get pv; kubectl get pvc;