-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove_pvc.sh
executable file
·37 lines (27 loc) · 1.12 KB
/
remove_pvc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash
# Get list of all PV names, skipping the header line
pv_list=$(kubectl get pv | tail -n +2 | awk '{print $1}')
# Check if any PVs were found
if [ -z "$pv_list" ]; then
echo "No PVs found"
exit 0
fi
# Process each PV
for pv in $pv_list; do
echo "Processing $pv..."
# Extract PVC name and namespace from PV
pvc_info=$(kubectl get pv $pv -o jsonpath='{.spec.claimRef.name}/{.spec.claimRef.namespace}')
pvc_name=$(echo $pvc_info | cut -d'/' -f1)
namespace=$(echo $pvc_info | cut -d'/' -f2)
echo "Removing finalizers from PVC $pvc_name in namespace $namespace..."
kubectl patch pvc $pvc_name -n $namespace -p '{"metadata":{"finalizers":null}}'
echo "Force deleting PVC $pvc_name..."
kubectl delete pvc $pvc_name -n $namespace --grace-period=0 --force
echo "Removing finalizers from PV $pv..."
kubectl patch pv $pv -p '{"metadata":{"finalizers":null}}'
echo "Force deleting PV $pv..."
kubectl delete pv $pv --grace-period=0 --force
echo "Completed processing $pv"
echo "------------------------"
done
echo "All PVs processed successfully"