Skip to content

Instantly share code, notes, and snippets.

@suhas316380
Created September 26, 2020 21:50
Show Gist options
  • Select an option

  • Save suhas316380/88e9350733a9ef7e03219f5a2124f144 to your computer and use it in GitHub Desktop.

Select an option

Save suhas316380/88e9350733a9ef7e03219f5a2124f144 to your computer and use it in GitHub Desktop.
kubectl - Delete all resources in all namespaces except certain namespaces
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
@suhas316380
Copy link
Author

Delete all resources in all namespaces excluding certain namespaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment