Skip to content

Instantly share code, notes, and snippets.

@superbrothers
Last active March 29, 2024 10:27
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save superbrothers/b428cd021e002f355ffd6dd421b75f70 to your computer and use it in GitHub Desktop.
Save superbrothers/b428cd021e002f355ffd6dd421b75f70 to your computer and use it in GitHub Desktop.
Kubernetes: Delete all objects in the namespace
kubectl delete "$(kubectl api-resources --namespaced=true --verbs=delete -o name | tr "\n" "," | sed -e 's/,$//')" --all
#!/usr/bin/env bash
#
# kubectl-delete_all is a utility to delete all objects in the namespace.
[[ -n "$DEBUG" ]] && set -x
set -eou pipefail
exec kubectl delete "$(kubectl api-resources --namespaced=true --verbs=delete -o name | tr "\n" "," | sed -e 's/,$//')" --all "$@"
@scheung38
Copy link

Hi @superbrothers: Does kubectl delete namespace jenkins for example does not delete all objects in jenkins namespace in this context?

@superbrothers
Copy link
Author

@scheung38 I did not understand your question. For example, the following command will delete all namespaced objects in jenkins namespace.

kubectl --namespace jenkins delete "$(kubectl api-resources --namespaced=true --verbs=delete -o name | tr "\n" "," | sed -e 's/,$//')" --all

@psdally
Copy link

psdally commented Mar 17, 2022

@scheung38's question, I think, is "Why bother with this plugin? Why not just delete the namespace itself, thereby implicitly deleting all of the objects within it?"

Depending on your roles and rolebindings, you might not have access to delete non-Namespaced objects (which a Namespace is), or re-create them. Sometimes Namespace deletions also get "stuck" on finalizers. So, deleting the contents of the Namespace without deleting the Namespace itself certainly is useful in some contexts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment