Skip to content

Instantly share code, notes, and snippets.

@ourway
Created November 18, 2025 04:05
Show Gist options
  • Select an option

  • Save ourway/021880da14509bcf09f932da1bbb0100 to your computer and use it in GitHub Desktop.

Select an option

Save ourway/021880da14509bcf09f932da1bbb0100 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Ubuntu Server Cleanup Script
# Run with: sudo ./cleanup.sh
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Please run with sudo: sudo ./cleanup.sh${NC}"
exit 1
fi
# Get the actual user (not root when using sudo)
ACTUAL_USER="${SUDO_USER:-$USER}"
ACTUAL_HOME=$(eval echo ~$ACTUAL_USER)
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Ubuntu Server Cleanup Script${NC}"
echo -e "${BLUE}========================================${NC}\n"
# Show initial disk usage
echo -e "${YELLOW}Initial Disk Usage:${NC}"
df -h / | grep -v Filesystem
journalctl --disk-usage 2>/dev/null || true
echo ""
TOTAL_FREED=0
# Function to show freed space
show_freed() {
local freed=$1
local description=$2
echo -e "${GREEN}✓ Freed: ${freed} - ${description}${NC}"
}
# 1. Clean Docker build cache
echo -e "\n${BLUE}[1/8] Cleaning Docker build cache...${NC}"
if command -v docker &> /dev/null; then
OUTPUT=$(docker builder prune -af 2>&1 | tail -1)
if echo "$OUTPUT" | grep -q "Total:"; then
FREED=$(echo "$OUTPUT" | grep -oP 'Total:\s+\K[\d.]+[KMGT]?B')
show_freed "$FREED" "Docker build cache"
else
echo -e "${YELLOW}No Docker build cache to clean${NC}"
fi
else
echo -e "${YELLOW}Docker not installed, skipping${NC}"
fi
# 2. Clean Docker images and volumes
echo -e "\n${BLUE}[2/8] Cleaning unused Docker images and volumes...${NC}"
if command -v docker &> /dev/null; then
OUTPUT=$(docker system prune -a --volumes -f 2>&1 | tail -1)
if echo "$OUTPUT" | grep -q "Total reclaimed space:"; then
FREED=$(echo "$OUTPUT" | grep -oP 'Total reclaimed space:\s+\K[\d.]+[KMGT]?B')
show_freed "$FREED" "Docker images and volumes"
else
echo -e "${YELLOW}No Docker images/volumes to clean${NC}"
fi
else
echo -e "${YELLOW}Docker not installed, skipping${NC}"
fi
# 3. Clean journal logs (keep last 7 days or max 500MB)
echo -e "\n${BLUE}[3/8] Cleaning old journal logs...${NC}"
BEFORE_JOURNAL=$(journalctl --disk-usage 2>/dev/null | grep -oP '[\d.]+[KMGT]?B' | head -1)
journalctl --vacuum-time=7d &>/dev/null || true
journalctl --vacuum-size=500M &>/dev/null || true
AFTER_JOURNAL=$(journalctl --disk-usage 2>/dev/null | grep -oP '[\d.]+[KMGT]?B' | head -1)
show_freed "Reduced from $BEFORE_JOURNAL to $AFTER_JOURNAL" "Journal logs"
# 4. Remove old snap revisions
echo -e "\n${BLUE}[4/8] Removing old snap revisions...${NC}"
if command -v snap &> /dev/null; then
SNAP_COUNT=0
snap list --all | awk '/disabled/{print $1, $3}' | while read snapname revision; do
if [ -n "$snapname" ] && [ -n "$revision" ]; then
snap remove "$snapname" --revision="$revision" 2>/dev/null && ((SNAP_COUNT++)) || true
fi
done
if [ $SNAP_COUNT -eq 0 ]; then
echo -e "${YELLOW}No old snap revisions to remove${NC}"
else
show_freed "$SNAP_COUNT revisions" "Old snap packages"
fi
else
echo -e "${YELLOW}Snap not installed, skipping${NC}"
fi
# 5. Clean apt cache
echo -e "\n${BLUE}[5/8] Cleaning apt package cache...${NC}"
BEFORE_APT=$(du -sh /var/cache/apt/archives 2>/dev/null | cut -f1)
apt clean &>/dev/null
apt autoclean &>/dev/null
AFTER_APT=$(du -sh /var/cache/apt/archives 2>/dev/null | cut -f1)
show_freed "Reduced from $BEFORE_APT to $AFTER_APT" "APT cache"
# 6. Remove unused packages
echo -e "\n${BLUE}[6/8] Removing unused packages...${NC}"
apt autoremove -y &>/dev/null
echo -e "${GREEN}✓ Removed unused packages${NC}"
# 7. Clean thumbnail cache
echo -e "\n${BLUE}[7/8] Cleaning thumbnail cache...${NC}"
if [ -d "$ACTUAL_HOME/.cache/thumbnails" ]; then
BEFORE_THUMB=$(du -sh "$ACTUAL_HOME/.cache/thumbnails" 2>/dev/null | cut -f1)
rm -rf "$ACTUAL_HOME/.cache/thumbnails"/*
AFTER_THUMB=$(du -sh "$ACTUAL_HOME/.cache/thumbnails" 2>/dev/null | cut -f1)
show_freed "Reduced from $BEFORE_THUMB to $AFTER_THUMB" "Thumbnail cache"
else
echo -e "${YELLOW}No thumbnail cache found${NC}"
fi
# 8. Clean /tmp and /var/tmp (optional - only old files)
echo -e "\n${BLUE}[8/8] Cleaning old temporary files (>7 days)...${NC}"
BEFORE_TMP=$(du -sh /tmp /var/tmp 2>/dev/null | awk '{sum+=$1} END {print sum}')
find /tmp -type f -atime +7 -delete 2>/dev/null || true
find /var/tmp -type f -atime +7 -delete 2>/dev/null || true
AFTER_TMP=$(du -sh /tmp /var/tmp 2>/dev/null | awk '{sum+=$1} END {print sum}')
echo -e "${GREEN}✓ Cleaned old temporary files${NC}"
echo -e "\n${BLUE}========================================${NC}"
echo -e "${BLUE}Optional Cleanups (User Caches)${NC}"
echo -e "${BLUE}========================================${NC}"
# Optional: NPM cache (uncomment to enable)
echo -e "\n${YELLOW}NPM cache: $ACTUAL_HOME/.npm${NC}"
if [ -d "$ACTUAL_HOME/.npm" ]; then
NPM_SIZE=$(du -sh "$ACTUAL_HOME/.npm" 2>/dev/null | cut -f1)
echo -e " Current size: $NPM_SIZE"
echo -e " To clean: ${BLUE}sudo -u $ACTUAL_USER npm cache clean --force${NC}"
fi
# Optional: Cargo cache (uncomment to enable)
echo -e "\n${YELLOW}Cargo cache: $ACTUAL_HOME/.cargo${NC}"
if [ -d "$ACTUAL_HOME/.cargo" ]; then
CARGO_SIZE=$(du -sh "$ACTUAL_HOME/.cargo" 2>/dev/null | cut -f1)
echo -e " Current size: $CARGO_SIZE"
echo -e " To clean: ${BLUE}cargo cache -a${NC} (requires cargo-cache: cargo install cargo-cache)"
fi
# Optional: Rustup toolchains (uncomment to enable)
echo -e "\n${YELLOW}Rustup toolchains: $ACTUAL_HOME/.rustup${NC}"
if [ -d "$ACTUAL_HOME/.rustup" ]; then
RUSTUP_SIZE=$(du -sh "$ACTUAL_HOME/.rustup" 2>/dev/null | cut -f1)
echo -e " Current size: $RUSTUP_SIZE"
echo -e " To list: ${BLUE}rustup toolchain list${NC}"
echo -e " To remove old: ${BLUE}rustup toolchain uninstall <toolchain>${NC}"
fi
# Optional: NVM node versions (uncomment to enable)
echo -e "\n${YELLOW}NVM node versions: $ACTUAL_HOME/.nvm${NC}"
if [ -d "$ACTUAL_HOME/.nvm" ]; then
NVM_SIZE=$(du -sh "$ACTUAL_HOME/.nvm" 2>/dev/null | cut -f1)
echo -e " Current size: $NVM_SIZE"
echo -e " To list: ${BLUE}nvm list${NC}"
echo -e " To remove old: ${BLUE}nvm uninstall <version>${NC}"
fi
# Show final disk usage
echo -e "\n${BLUE}========================================${NC}"
echo -e "${BLUE}Cleanup Complete!${NC}"
echo -e "${BLUE}========================================${NC}\n"
echo -e "${GREEN}Final Disk Usage:${NC}"
df -h / | grep -v Filesystem
echo ""
journalctl --disk-usage 2>/dev/null || true
echo -e "\n${GREEN}✓ Cleanup completed successfully!${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment