Kubernetes PersistentVolume: Data cleanup on object deletion

2 min read | by Jordi Prats

A PersistentVolume is the abstract representation of storage as a resource within a Kubernetes cluster. The attributes describe the storage resource, it's underlying resource can either be a disk provisioned by the cloud you are using or something as simple as manually provisioned NFS disk

On PersistentVolumes we can configure several reclaim policies to define how it's data removal is handled once the PersistentVolume object is deleted.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: demo-pv
spec:
  accessModes:
  - ReadWriteOnce
  awsElasticBlockStore:
    fsType: ext4
  capacity:
    storage: 10Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: gp2
  volumeMode: Filesystem

The persistentVolumeReclaimPolicy determines how the storage resources can be reused when it is no longer needed:

  • Retain: Data will not be deleted unless manually requested
  • Recycle: Deletes all of the data by removing it's files. This can be useful to reuse a disk that is not dynamically provisioned, for example a NFS disk
  • Delete: Deletes the underlying storage (only applies on cloud storage, some StorageClasses won't be able to delete the underlying disk)

Posted on 12/01/2022