Last active
October 24, 2025 15:12
-
-
Save mablue/f59b3b1d901efb07442851774f42577e to your computer and use it in GitHub Desktop.
Complete CUDA Cleanup Script (Debian)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| echo "=== SAFE CUDA/NVIDIA CLEANUP ===" | |
| echo "This script will only remove CUDA and NVIDIA-related packages" | |
| echo "System packages like NetworkManager, desktop environments, and icons will be preserved" | |
| echo "" | |
| # Function to safely remove packages with pattern matching | |
| safe_remove_packages() { | |
| local pattern=$1 | |
| local description=$2 | |
| echo "=== Checking for $description ===" | |
| # Get all packages matching the pattern, but exclude critical system packages | |
| packages=$(dpkg -l | grep -i "$pattern" | awk '{print $2}' | \ | |
| grep -v -E '^(networkmanager|gnome-shell|kde-plasma|xfce4|ubuntu-desktop|linux-generic|xserver-xorg|mesa|libgl|libegl|libgles)') | |
| if [ -n "$packages" ]; then | |
| echo "Found packages to remove:" | |
| echo "$packages" | |
| echo "" | |
| read -p "Proceed with removing these packages? (y/N): " confirm | |
| if [[ $confirm =~ ^[Yy]$ ]]; then | |
| sudo apt-get purge -y $packages | |
| else | |
| echo "Skipping removal of $description" | |
| fi | |
| else | |
| echo "No packages found for: $description" | |
| fi | |
| echo "" | |
| } | |
| # Stop only NVIDIA services (not system services) | |
| echo "Stopping NVIDIA services..." | |
| sudo systemctl stop nvidia-persistenced 2>/dev/null || true | |
| sudo systemctl disable nvidia-persistenced 2>/dev/null || true | |
| sudo systemctl stop nvidia-powerd 2>/dev/null || true | |
| sudo systemctl disable nvidia-powerd 2>/dev/null || true | |
| # Remove NVIDIA/CUDA packages with safety checks | |
| safe_remove_packages "^nvidia-[0-9]+" "NVIDIA driver packages" | |
| safe_remove_packages "^cuda-" "CUDA toolkit packages" | |
| safe_remove_packages "nvidia-cuda" "NVIDIA CUDA packages" | |
| safe_remove_packages "libcublas" "CUDA BLAS libraries" | |
| safe_remove_packages "libcufft" "CUDA FFT libraries" | |
| safe_remove_packages "libcurand" "CUDA RAND libraries" | |
| safe_remove_packages "libcusolver" "CUDA solver libraries" | |
| safe_remove_packages "libcusparse" "CUDA sparse matrix libraries" | |
| safe_remove_packages "libnpp" "NVIDIA Performance Primitives" | |
| safe_remove_packages "nsight" "NVIDIA Nsight tools" | |
| safe_remove_packages "nvrtc" "CUDA Runtime Compilation" | |
| safe_remove_packages "nvjpeg" "NVIDIA JPEG library" | |
| safe_remove_packages "cupti" "CUDA Profiling Tools Interface" | |
| safe_remove_packages "cudnn" "CUDA Deep Neural Networks" | |
| # Remove specific CUDA development packages | |
| echo "=== Checking for CUDA development packages ===" | |
| dev_packages=$(dpkg -l | awk '{print $2}' | grep -E '^(libcu|libnv)' | \ | |
| grep -v -E '(networkmanager|gnome|kde|xfce|ubuntu-desktop)') | |
| if [ -n "$dev_packages" ]; then | |
| echo "Found development packages:" | |
| echo "$dev_packages" | |
| read -p "Remove these development packages? (y/N): " confirm | |
| if [[ $confirm =~ ^[Yy]$ ]]; then | |
| sudo apt-get purge -y $dev_packages | |
| fi | |
| fi | |
| # Clean up only if we actually removed anything | |
| echo "Cleaning up dependencies..." | |
| sudo apt-get autoremove -y | |
| sudo apt-get autoclean -y | |
| # Remove only known CUDA/NVIDIA directories (safer approach) | |
| echo "Removing CUDA/NVIDIA directories..." | |
| sudo rm -rf /usr/local/cuda-* 2>/dev/null || true | |
| sudo rm -rf /opt/nvidia 2>/dev/null || true | |
| sudo rm -rf /opt/cuda 2>/dev/null || true | |
| # Remove only specific library directories that are definitely NVIDIA/CUDA | |
| for dir in /usr/lib/x86_64-linux-gnu /usr/lib64; do | |
| if [ -d "$dir" ]; then | |
| sudo find "$dir" -name "*nvidia*" -type f -delete 2>/dev/null || true | |
| sudo find "$dir" -name "*cuda*" -type f -delete 2>/dev/null || true | |
| fi | |
| done | |
| # Remove only specific include directories | |
| sudo rm -rf /usr/local/cuda 2>/dev/null || true | |
| sudo find /usr/include -name "*cuda*" -type f -delete 2>/dev/null || true | |
| sudo find /usr/include -name "*nvidia*" -type f -delete 2>/dev/null || true | |
| # Clean package cache (NVIDIA/CUDA only) | |
| echo "Cleaning NVIDIA/CUDA package cache..." | |
| sudo find /var/cache/apt/archives -name "*nvidia*" -type f -delete 2>/dev/null || true | |
| sudo find /var/cache/apt/archives -name "*cuda*" -type f -delete 2>/dev/null || true | |
| # Remove only NVIDIA/CUDA repositories | |
| echo "Removing NVIDIA/CUDA repositories..." | |
| sudo rm -f /etc/apt/sources.list.d/cuda*.list 2>/dev/null || true | |
| sudo rm -f /etc/apt/sources.list.d/nvidia*.list 2>/dev/null || true | |
| sudo rm -f /etc/apt/sources.list.d/graphics-drivers*.list 2>/dev/null || true | |
| # Update package list | |
| sudo apt-get update | |
| # Clean environment variables (user only, not system) | |
| echo "Cleaning user environment variables..." | |
| sed -i '/CUDA_HOME/d' ~/.bashrc ~/.profile ~/.bash_profile 2>/dev/null || true | |
| sed -i '/CUDA_PATH/d' ~/.bashrc ~/.profile ~/.bash_profile 2>/dev/null || true | |
| sed -i '/cuda.*bin/d' ~/.bashrc ~/.profile ~/.bash_profile 2>/dev/null || true | |
| sed -i '/usr.local.cuda/d' ~/.bashrc ~/.profile ~/.bash_profile 2>/dev/null || true | |
| # Blacklist NVIDIA modules (optional) | |
| read -p "Blacklist NVIDIA modules? (prevents auto-loading) (y/N): " blacklist_confirm | |
| if [[ $blacklist_confirm =~ ^[Yy]$ ]]; then | |
| echo "Blacklisting NVIDIA modules..." | |
| sudo tee /etc/modprobe.d/blacklist-nvidia.conf << EOF | |
| blacklist nvidia | |
| blacklist nvidia-drm | |
| blacklist nvidia-modeset | |
| blacklist nvidia-uvm | |
| blacklist nouveau | |
| EOF | |
| sudo update-initramfs -u | |
| fi | |
| # Reload shell environment | |
| source ~/.bashrc 2>/dev/null || true | |
| echo "=== SAFE CLEANUP COMPLETE ===" | |
| echo "System components like NetworkManager and desktop environment were preserved." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment