Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Created March 4, 2019 22:43
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 jaytaylor/1d0695cbe42f1b818872a732e02205ba to your computer and use it in GitHub Desktop.
Save jaytaylor/1d0695cbe42f1b818872a732e02205ba to your computer and use it in GitHub Desktop.
Delete all resources from a Kubernetes namespace.
#!/usr/bin/env bash
#
# @author Jay Taylor [jaytaylor.com](https://jaytaylor.com)
#
# @date 2019-03-04
#
# Sometimes Kubernetes seems to get stuck and doesn't
# delete the contents of a namespace from my cluster after
# `kubectl delete namespace X` has been run.
#
# This small program lists and deletes each resource from
# the specified namespace.
#
# Also see ctron's "kill-kube-ns", you may need it as a
# last step:
#
# https://github.com/ctron/kill-kube-ns
#
set -o errexit
set -o pipefail
set -o nounset
if [[ "${DEBUG:-}" =~ ^1|[tT](rue)?|[oO]n?|[yY](es)?$ ]] ; then
set -o xtrace
fi
if [ -z "${1:-}" ] ; then
echo 'ERROR: missing required argument: namespace' 1>&2
exit 1
fi
ns="$1"
kubectl get all -o json --namespace "${ns}" \
| jq -r '.items[] | .kind + "/" + .metadata.name' \
| xargs -n1 -IX kubectl delete X --namespace "${ns}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment