Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.