I want to delete all the Persistent Volume Claims attached to a customized deployment. At first, I delete the deployment itself that deletes all the pods but the disks aren't deleted.
I know the command to delete pvcs but that involves fetching all of them using a command[1] and then manually copying-pasting[2] the ones that belong to the deployment set.
If I want to do this programmatically, which API of Kubernetes Python client or the cli command do I use?
I am using GKE.
I tried reading the Python client documentation but could not deduce how to do this task.
[1] kubectl --namespace=<my_namespace> --context=<my_cluster> get pvc
[2] kubectl --namespace=<my_namespace> --context=<cluster_name> delete pvc data-avirals-july19-1-devel-0 data-avirals-july19-1-devel-1
There might be some shorter way, however this one would filter all the pvc associated with the deployment name MY_DEPLOYMENT_NAME
. Other pvc
(not associated with the same deployment) will not be touched.
for volume in $(kubectl get deployments MY_DEPLOYMENT_NAME -o jsonpath='{.spec.template.spec.volumes[*].name}');
do
kubectl get pv $volume -o jsonpath='{.spec.claimRef.name}{"\n"}' ;
# kubectl delete pvc "$(kubectl get pv $volume -o jsonpath='{.spec.claimRef.name}{"\n"}')"
done
Note:
kubectl delete
to really delete the pvc. Its commented to protect users from accidently deleting.bash
is required to make this loop work. Explanation:
Following command would list down the volumes used by a deployment called MY_DEPLOYMENT_NAME
kubectl get deployments MY_DEPLOYMENT_NAME -o jsonpath='{.spec.template.spec.volumes[*].name}'
Now,we will loop over these volumes, to find what is the claim
used by these volumes. This is done my checking claimRef
section. This is done by:
kubectl get pv $volume -o jsonpath='{.spec.claimRef.name}{"\n"}'
You can delete all pvc at once with the following command:
kubectl --namespace=<my_namespace> delete pvc --all
The PVs should be deleted after that but it might take some time. Also, be very careful with this command and make sure you are deleting what you intend to.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.