Skip to content

Instantly share code, notes, and snippets.

@Aldo-f
Created February 9, 2026 19:53
Show Gist options
  • Select an option

  • Save Aldo-f/21924e662fbced6473d5fbb4423633a2 to your computer and use it in GitHub Desktop.

Select an option

Save Aldo-f/21924e662fbced6473d5fbb4423633a2 to your computer and use it in GitHub Desktop.
Cleanup linux
#!/usr/bin/env bash
set -e
# Functions
get_free() { df -B1 --output=avail / | tail -1; }
human() { numfmt --to=iec --suffix=B "$1"; }
# Colors
GREEN="\e[32m"
YELLOW="\e[33m"
RED="\e[31m"
RESET="\e[0m"
echo -e "${YELLOW}=== BEFORE CLEANUP ===${RESET}"
df -h /
docker system df || true
echo
before=$(get_free)
# --- APT cleanup ---
echo -e "${GREEN}Cleaning up apt packages...${RESET}"
sudo apt autoremove -y
sudo apt autoclean -y
sudo apt clean
# --- Docker cleanup ---
echo -e "${GREEN}Cleaning up Docker...${RESET}"
docker system prune -a --volumes -f || true
# --- Journal logs cleanup ---
echo -e "${GREEN}Cleaning up old journal logs (older than 7 days)...${RESET}"
sudo journalctl --vacuum-time=7d || true
# --- Snap cache ---
if command -v snap &>/dev/null; then
echo -e "${GREEN}Cleaning Snap cache...${RESET}"
sudo rm -rf /var/lib/snapd/cache/* || true
fi
# --- npm cache ---
if command -v npm &>/dev/null; then
echo -e "${GREEN}Cleaning npm cache...${RESET}"
npm cache clean --force || true
fi
# --- pnpm cache ---
if command -v pnpm &>/dev/null; then
echo -e "${GREEN}Cleaning pnpm cache...${RESET}"
pnpm store prune || true
fi
# --- pip cache ---
if command -v pip &>/dev/null; then
echo -e "${GREEN}Cleaning pip cache...${RESET}"
pip cache purge || true
fi
# --- Old kernels (Ubuntu/Debian) ---
if command -v purge-old-kernels &>/dev/null; then
echo -e "${GREEN}Removing old kernels...${RESET}"
sudo purge-old-kernels -y || true
fi
# --- Done, report ---
after=$(get_free)
freed=$((after - before))
echo -e "\n${YELLOW}=== AFTER CLEANUP ===${RESET}"
df -h /
docker system df || true
if [ "$freed" -gt 0 ]; then
echo -e "${GREEN}You cleaned up $(human "$freed")${RESET}"
else
echo -e "${RED}No space recovered${RESET}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment