Skip to content

Instantly share code, notes, and snippets.

@bertrandmartel
Created October 24, 2022 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bertrandmartel/22c6e8e4515513a416ce61a76a404a4b to your computer and use it in GitHub Desktop.
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
#!/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