Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 353 of 8025. Advanced Kubernetes ConfigurationFree lesson

Executing Clean Deletion of Stateful Workloads

Perform proper teardown procedures to safely release persistent volumes and reclaim infrastructure resources after decommissioning a stateful application.

In this lesson, you will learn how to systematically decommission a stateful application to ensure your persistent data and infrastructure resources are released safely and completely.

When you delete a StatefulSet, Kubernetes gracefully shuts down the pods, but it does not automatically delete the associated PersistentVolumeClaims (PVCs). This is a safety feature designed to prevent accidental data loss. If you simply run kubectl delete statefulset my-app, your pods will disappear, but the underlying storage (and the costs associated with it) will remain indefinitely.

The Cleanup Workflow

To perform a clean deletion, you must follow a deliberate order of operations:

  1. Delete the StatefulSet: Removing the workload stops the pods and halts new data writes. kubectl delete statefulset <name>
  2. Delete the Headless Service: Since the service was only needed to provide predictable network identity to the stateful pods, you should remove it once the pods are gone. kubectl delete service <service-name>
  3. Delete the PersistentVolumeClaims: Once you have verified that you no longer need the data (or have completed your backups using the backup automation we discussed earlier), you must manually remove the claims to trigger the release of the storage. kubectl delete pvc <pvc-name>

Why order matters

If you attempt to delete the PVCs while the StatefulSet is still running, the pods may enter a Terminating state indefinitely because they cannot unmount a volume that has been pulled out from under them. Conversely, by deleting the StatefulSet first, you ensure the pods are gracefully unmounted from the volumes, allowing the CSI driver to clean up the attachment properly.

By following this sequence, you ensure that your cloud provider’s block storage is reclaimed, preventing "orphaned" volumes from inflating your infrastructure bill. Always verify that your final backups are confirmed before deleting the PVCs, as this is the point of no return for your application data.

Key Takeaway: Clean deletion of stateful workloads requires a manual, ordered teardown where the StatefulSet is terminated before the PersistentVolumeClaims are removed to ensure data integrity and proper resource reclamation.