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.

Revisions

  1. gitnik created this gist May 22, 2018.
    32 changes: 32 additions & 0 deletions kubectl-deletion-helper.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/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"
    }