Created
September 26, 2020 21:50
-
-
Save suhas316380/88e9350733a9ef7e03219f5a2124f144 to your computer and use it in GitHub Desktop.
kubectl - Delete all resources in all namespaces except certain namespaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| timestamp=$(date +%d-%m-%Y_%H-%M-%S) | |
| echo "############## ${timestamp} ##############" | |
| declare -a ignore_namespaces=("kube-system" "kube-node-lease" "kube-public") | |
| namespaces=($(/usr/local/bin/kubectl get ns -o name)) | |
| for ns in "${namespaces[@]}"; do | |
| ns=${ns##*/} | |
| echo -e "\nProcessing Namespace: ${ns} " | |
| status=$(/usr/local/bin/kubectl get ns ${ns} -o jsonpath='{.status.phase}') | |
| if [ "${status}" == "Active" ]; then | |
| if grep -q "${ns}" <<< "${ignore_namespaces[@]}" | |
| then | |
| echo "Namespace: ${ns} present in Ignore Namespaces array..skipping" | |
| else | |
| /usr/local/bin/kubectl -n ${ns} delete all --all | |
| fi | |
| else | |
| echo "Namespace: ${ns} in status: ${status} ...skipping" | |
| fi | |
| done |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Delete all resources in all namespaces excluding certain namespaces