Auto delete persistant volume claim when a kubernetes job gets completed
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In Kubernetes, the cleanup of resources after a task or job finishes is an essential aspect of resource management and efficiency. One such consideration is the persistent volume claim (PVC) associated with a Kubernetes job. A PVC is a request for storage by a user which can be provisioned either manually or dynamically. While PVCs are generally persistent, sometimes, you might want them to be deleted automatically once the job using them is completed. This article delves into strategies and solutions for automatically deleting a PVC after its associated job is completed.
Understanding Persistent Volume Claims (PVCs)
A Persistent Volume Claim is an abstraction over storage resources, allowing pods to request and consume either dynamically or statically provisioned storage. PVCs enable data persistence beyond the lifecycle of any particular pod.
Key Characteristics of PVCs:
- Persistent Lifecycle: Unlike ephemeral storage which is only available for the pod's lifecycle, PVCs persist even after the pod has been deleted.
- Dynamic Provisioning: PVCs can automatically provision persistent volumes (PV) using Storage Classes allowing for granular and flexible storage management.
- Binding and Reclaiming: PVC binds to a PV with a definite `Reclaim Policy`, which dictates the fate of the PV once it is no longer needed by the claim.
Automatically Deleting PVCs Post Job Completion
Kubernetes jobs are typically used for batch operations. However, once a job completes, the associated resources such as PVCs might remain unless explicitly cleaned up. Here are some general strategies for automating the deletion of PVCs:
1. Finalizers
Finalizers provide a delay in the deletion of an object until specific conditions are met. A Job or Pod can be equipped with a custom finalizer to ensure that associated PVCs are deleted or handled appropriately.
Example Implementation:
Here’s a naive implementation using a job and an external script to delete the PVC:
- name: job-storage
- name: example
- apiVersion: batch/v1
- ReadWriteOnce
- name: cleanup
- Reclaim Policies: PVCs also involve `Reclaim Policies` such as `Retain`, `Delete`, or `Recycle` affecting PV behavior. Be sure to set these correctly when using dynamic provisioning.
- Team Coordination: Teams should coordinate resource management according to organization policies to avoid unintended data loss.
- Resource Monitoring: Implement monitoring solutions to keep an eye on resource usage and optimize storage costs.
Related reading
- Avoid multiple cron jobs running for one cron execution point in Kubernetes
- AWS-ECS vs Kubernetes
- AWS ECS Fargate and port mapping
- AWS EKS NodeGroup Create failed Instances failed to join the kubernetes cluster
- AWS EKS NodeGroup Create failed Instances failed to join the kubernetes cluster
- AWS Nginx Ingress creating Classic Load Balancer instead of Application Load Balancer
- Azure Functions Kubernetes cannot find local.settings.json
- Azure Kubernetes Service Setup an Internal load balancer with static IP address

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.