Skip to content

Instantly share code, notes, and snippets.

@superseb
Last active November 26, 2025 14:21
Show Gist options
  • Select an option

  • Save superseb/2cf186726807a012af59a027cb41270d to your computer and use it in GitHub Desktop.

Select an option

Save superseb/2cf186726807a012af59a027cb41270d to your computer and use it in GitHub Desktop.
Cleanup host added as custom to Rancher 2.0
#!/bin/sh
# OUTDATED: please refer to the link below for the latest version:
# https://github.com/rancherlabs/support-tools/blob/master/extended-rancher-2-cleanup/extended-cleanup-rancher2.sh
docker rm -f $(docker ps -qa)
docker volume rm $(docker volume ls -q)
cleanupdirs="/var/lib/etcd /etc/kubernetes /etc/cni /opt/cni /var/lib/cni /var/run/calico /opt/rke"
for dir in $cleanupdirs; do
echo "Removing $dir"
rm -rf $dir
done
@vyo

vyo commented Aug 10, 2018

Copy link
Copy Markdown

life saver =)

@steve-todorov

Copy link
Copy Markdown

Is there a way to put all of these in a single place other than creating a link with ln -s?

@tgruenert

Copy link
Copy Markdown

This script delete Rancher2 nodes in a clean way and prepare a recycling / redeployment of nodes in a perfect manner. Little trick - great solution. Thank you.

(Typing some words about will give other users a chance to find this solution. Did it here for all of us ;-) ).

@moisei

moisei commented Dec 19, 2018

Copy link
Copy Markdown

I'll keep it here for myself :)!
curl https://gist.githubusercontent.com/superseb/2cf186726807a012af59a027cb41270d/raw/eaa2d235e7693c2d1c5a2a916349410274bb95a9/cleanup.sh > ./cleanup.sh && chmod a+x ./cleanup.sh && sudo ./cleanup.sh

@takatost

takatost commented Mar 7, 2019

Copy link
Copy Markdown

It works, thx. You are my hero!!

@davidjsanders

Copy link
Copy Markdown

Thank you for this, much appreciated!

I made a slight adaptation of this script and called it clean_rancher.sh after forgetting to run it as sudo, so pasting here in case it helps anyone else...

#!/bin/bash
user=$EUID
if [ "${user}" != "0" ]; then
  echo
  echo "$0 must be run as root - you are running as $EUID"
  echo
  exit 1
fi
echo
echo "About to destroy Rancher 2.x install"
echo "5s to cancel with ^c"
echo
sleep 5

containers=$(docker ps -aq)
if [ "${containers}x" != "x" ]
then
  docker rm -f $(docker ps -qa)
else
  echo "No running containers - ignoring docker rm"
fi

volumes=$(docker volume ls -q)
if [ "${volumes}x" != "x" ]
then
  docker volume rm $(docker volume ls -q)
else
  echo "No volumes - ignoring docker volume rm"
fi

cleanupdirs="/var/lib/etcd /etc/kubernetes /etc/cni /opt/cni /var/lib/cni /var/run/calico /opt/rke"
for dir in $cleanupdirs; do
  echo "Removing $dir"
  rm -rf $dir
done

Then, chmod +x clean_rancher.sh

Hope this helps others and thanks again for the original script!

David

@wolkjesmaker

Copy link
Copy Markdown

Thanks, saved me some trouble! 👍

@zzkgo

zzkgo commented Apr 12, 2019

Copy link
Copy Markdown

awesome work!

ghost commented Jun 16, 2019

Copy link
Copy Markdown

this repo is a hidden gem

@dinhanhhuy

Copy link
Copy Markdown

Is this command rm all container in docker (event it doesn't relate, and installed before Rancher cluster)?

@Snaacker

Copy link
Copy Markdown

Is this command rm all container in docker (event it doesn't relate, and installed before Rancher cluster)?

It will remove all running container on your machine (even it's not related to Rancher cluster)

@sdirkwinkel

Copy link
Copy Markdown

I've changed the script to just remove rancher / k8s containers and images and use docker volume prune to cleanup volumes:

#!/bin/bash
user=$EUID
if [ "${user}" != "0" ]; then
  echo
  echo "$0 must be run as root - you are running as $EUID"
  echo
  exit 1
fi
echo
echo "About to destroy Rancher 2.x install"
echo "5s to cancel with ^c"
echo
sleep 5

containers=$(docker ps -a | grep -E "rancher|k8s" | awk '{print $1}')
if [ "${containers}x" != "x" ]
then
  docker rm -f $containers
else
  echo "No containers - ignoring docker rm"
fi

images=$(docker images -a | grep -E "rancher|k8s" | awk '{print $3}')
if [ "${images}x" != "x" ]
then
  docker rmi $images
else
  echo "No images - ignoring docker rmi"
fi

docker volume prune


cleanupdirs="/var/lib/etcd /etc/kubernetes /etc/cni /opt/cni /var/lib/cni /var/run/calico /opt/rke"
for dir in $cleanupdirs; do
  echo "Removing $dir"
  rm -rf $dir
done

@busla

busla commented Oct 29, 2019

Copy link
Copy Markdown

When installing rancher-agent on the same host for testing purposes the cluster would not run properly until I also removed all data from /opt/rancher, perhaps it was only one of the three dirs:

root@rancher:~# ls /opt/rancher
certs-cache  k3s  management-state

@qrzeller

qrzeller commented May 4, 2020

Copy link
Copy Markdown

Thanks !
ros should have a clean function. Or reinstall from cloud-config...

@mikekuzak

Copy link
Copy Markdown

Will this unregister the node from the cluster too ?

@awesinine

Copy link
Copy Markdown

saved me a bunch of time, tyvm!

@HoffmannP

Copy link
Copy Markdown

Are you shure it's /var/run/calico and not /var/lib/calico?

@superseb

Copy link
Copy Markdown
Author

@HoffmannP This gist is outdated, I updated the code with a link to the updated version.

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