Search code examples
kubernetespersistent-volume-claims

Auto delete PVC when scaling down?


I am looking for a way to delete PersistentVolumeClaims assigned to pods of a StatefulSet automatically when I scale down the number of instances. Is there a way to do this within k8s? I haven't found anything in the docs, yet.


Solution

  • I suspect that a preStop Lifecycle Handler could submit a Job to clean up the PVC, assuming the Pod's ServiceAccount had the Role to do so. Unfortunately, the Lifecycle Handler docs say that the exec blocks the Pod deletion, so that's why whatever happened would need to be asynchronous from the Pod's perspective.

    Another approach might be to unconditionally scan the cluster or namespace with a CronJob and delete unassigned PVCs, or those that match a certain selector.

    But I don't think there is any inherent ability to do that, given that (at least in my own usage) it's reasonable to scale a StatefulSet up and down, and when scaling it back up then one would actually desire that the Pod regain its identity in the StatefulSet, which typically includes any persisted data.