Skip to content

Instantly share code, notes, and snippets.

@aaiezza
Created December 13, 2024 15:19
Show Gist options
  • Save aaiezza/4f16a14be45084c45125c8ae99fbdd8e to your computer and use it in GitHub Desktop.
Save aaiezza/4f16a14be45084c45125c8ae99fbdd8e to your computer and use it in GitHub Desktop.
Wipe local Docker & Kubernetes environment clean
#!/bin/bash
# This script will wipe Docker and Kubernetes clean. USE WITH CAUTION.
set -e
# Function to confirm action
confirm() {
read -r -p "$1 [y/N]: " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
echo "Aborted."
exit 1
;;
esac
}
confirm "This script will wipe Docker and Kubernetes clean. Are you sure you want to proceed?"
# Clean Docker
echo "Stopping all Docker containers..."
docker stop $(docker ps -q) || true
echo "Removing all Docker containers..."
docker rm $(docker ps -aq) || true
echo "Removing all Docker images..."
docker rmi $(docker images -q) --force || true
echo "Removing all Docker volumes..."
docker volume rm $(docker volume ls -q) || true
echo "Removing all Docker networks (except default)..."
docker network rm $(docker network ls | grep -vE 'bridge|host|none' | awk '{print $1}') || true
echo "Removing all Docker builds..."
docker builder prune -a -f
echo "Pruning Docker system..."
docker system prune -a --volumes -f
echo "Docker cleanup complete."
# Clean Kubernetes
echo "Deleting all Kubernetes resources in all namespaces..."
kubectl delete all --all --all-namespaces || true
echo "Deleting all Kubernetes PVCs..."
kubectl delete pvc --all --all-namespaces || true
echo "Deleting all Kubernetes PVs..."
kubectl delete pv --all || true
echo "Deleting all Kubernetes namespaces (except default ones)..."
kubectl delete namespace $(kubectl get namespaces | grep -vE 'default|kube-system|kube-public' | awk '{print $1}') || true
if command -v kubeadm &> /dev/null; then
echo "Resetting Kubernetes cluster..."
kubeadm reset -f || true
fi
echo "Removing Kubernetes configuration files..."
rm -rf ~/.kube
echo "Kubernetes cleanup complete."
# Final message
echo "Docker and Kubernetes have been wiped clean."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment