Created
October 24, 2022 13:12
-
-
Save bertrandmartel/22c6e8e4515513a416ce61a76a404a4b to your computer and use it in GitHub Desktop.
Script to delete all resources of a specific type, in a specific namespace in a kubernetes cluster
This file contains 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
#!/bin/bash | |
usage() { echo "Usage: $0 [-n <string>] [-t <string>]" 1>&2; exit 1; } | |
while getopts ":n:t:" o; do | |
case "${o}" in | |
n) | |
namespace=${OPTARG} | |
;; | |
t) | |
type=${OPTARG} | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ -z "${namespace}" ] || [ -z "${type}" ]; then | |
usage | |
fi | |
echo "deleting all resources with type '$type' in namespace '$namespace'"; | |
if [ -z $namespace ]; then | |
echo "namespace argument -n is required. $usage" | |
exit 1 | |
fi | |
if [ -z $type ]; then | |
echo "resource type argument -t is required. $usage" | |
exit 1 | |
fi | |
resources_arr=($(kubectl get $type -n $namespace --no-headers -o custom-columns=":metadata.name")) | |
for i in "${resources_arr[@]}" | |
do | |
kubectl delete $type "$i" -n $namespace | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment