Skip to content

Instantly share code, notes, and snippets.

@gitnik
Created May 22, 2018 08:23
Show Gist options
  • Save gitnik/59cb416d4e1b1589cfe79d50287bd6ab to your computer and use it in GitHub Desktop.
Save gitnik/59cb416d4e1b1589cfe79d50287bd6ab to your computer and use it in GitHub Desktop.
kubectl deletion helper
#!/usr/bin/env bash
set -e
kubedl ()
{
TYPE=$1
NAME=$2
NAMESPACE=$3
if [ -z "$TYPE" ] || [ -z "$NAME" ]; then
echo "Usage: kubedl <type> <name/label> <namespace=default>"
return
fi
if [ -z "$NAMESPACE" ]; then
NAMESPACE="default"
fi
if [[ "$NAME" =~ "=" ]]; then
OBJECT=$(kubectl get "$TYPE" --no-headers --namespace="$NAMESPACE" --ignore-not-found | grep "$NAME" | awk '{print $1}')
else
OBJECT=$(kubectl get "$TYPE" --no-headers --namespace="$NAMESPACE" -l "$NAME" --ignore-not-found | awk '{print $1}')
fi
if [ -z "$OBJECT" ]; then
echo "$TYPE not found with name/label $NAME"
return
fi
kubectl delete "$TYPE" "$OBJECT" --namespace="$NAMESPACE"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment