Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Delete all resources from a Kubernetes namespace.
#!/usr/bin/env bash
# @author Jay Taylor [](
# @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:
set -o errexit
set -o pipefail
set -o nounset
if [[ "${DEBUG:-}" =~ ^1|[tT](rue)?|[oO]n?|[yY](es)?$ ]] ; then
set -o xtrace
if [ -z "${1:-}" ] ; then
echo 'ERROR: missing required argument: namespace' 1>&2
exit 1
kubectl get all -o json --namespace "${ns}" \
| jq -r '.items[] | .kind + "/" +' \
| 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.