Auto delete persistant volume claim when a kubernetes job gets completed

6/25/2018

Is there any policy or a way that enable us to auto delete a persistent volume claim when a kubernetes-job (using that claim) gets completed.

FYI: i am creating persistent volume claim using delete policy i.e. on claim deletion the associated persistent volume gets deleted too which is my intended behavior.

only suggest other solutions like (cronjobs for cleaning or kubernetes watch feature) if there is not automated way of doing this, that will be my last option.

-- UmairAhmad
kubernetes
kubernetes-pvc
persistent-volumes

1 Answer

6/26/2018

There is no such functionality in Kubernetes to delete a Persistent Volume Claim after a Job has been completed.

One of the ways is to use Cron Job with simple busybox container running bash script, which checks and deletes unused Persistent Volume Claims.

It is not recommended to use custom scripts in "pre-stop phase" in Pod as @techtrainer suggested in comments because if some errors appear before this script, Job will fail and Persistent Volume Claim won’t be deleted.

-- Artem Golenyaev
Source: StackOverflow