Disk Space Management
This guide explains how to analyze, clean, and optimize disk space on your VPS.
Analyze Disk Usage
Overview
df -h
Example:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 35G 15G 70% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
Detail by Folder
# Size of main folders
du -h --max-depth=1 / 2>/dev/null | sort -hr | head -15
Typical result:
15G /var
8G /usr
5G /home
3G /opt
Analyze a Specific Folder
# Analyze /var
du -h --max-depth=1 /var | sort -hr
# Analyze /home
du -h --max-depth=1 /home | sort -hr
Interactive Tool: ncdu
# Install
sudo apt install ncdu
# Analyze
sudo ncdu /
# Navigation: arrows, Enter to go in, d to delete
Clean the System
APT Packages
# Remove orphan packages
sudo apt autoremove -y
# Empty APT cache
sudo apt clean
# Remove old kernels
sudo apt autoremove --purge -y
Space recovered: ~500MB to 2GB
Cache and Temporary Files
# Temporary files
sudo rm -rf /tmp/*
sudo rm -rf /var/tmp/*
# Thumbnail cache (if desktop)
rm -rf ~/.cache/thumbnails/*
System Logs
# Current log size
du -h /var/log/
# Clean systemd journals (keep 7 days)
sudo journalctl --vacuum-time=7d
# Or keep max 500MB
sudo journalctl --vacuum-size=500M
# Delete old compressed logs
sudo find /var/log -name "*.gz" -delete
sudo find /var/log -name "*.1" -delete
sudo find /var/log -name "*.old" -delete
Log Rotation
Configure automatic rotation:
sudo nano /etc/logrotate.d/custom
/var/log/*.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
}
Clean Docker
If you use Docker:
# View usage
docker system df
# Remove unused resources
docker system prune -af
# Also remove unused volumes
docker system prune -af --volumes
# Remove untagged images
docker image prune -af
Warning
docker system prune --volumes deletes unattached volumes. Verify you don't have important data.
Clean Applications
Node.js / npm
# npm cache
npm cache clean --force
# Unused node_modules
find /home -name "node_modules" -type d -exec du -sh {} \; 2>/dev/null
# Delete those no longer needed
rm -rf /path/to/project/node_modules
Python / pip
# pip cache
pip cache purge
# Or manually
rm -rf ~/.cache/pip
Composer (PHP)
# Composer cache
composer clear-cache
rm -rf ~/.composer/cache
Find Large Files
# Files > 100MB
find / -type f -size +100M 2>/dev/null | xargs ls -lh
# Files > 500MB
find / -type f -size +500M 2>/dev/null | xargs ls -lh
# Top 20 largest files
find / -type f -exec du -h {} + 2>/dev/null | sort -rh | head -20
Priority Files to Check
Application Logs
# Nginx logs
du -h /var/log/nginx/
# Apache logs
du -h /var/log/apache2/
# MySQL logs
du -h /var/log/mysql/
Forgotten Backups
# Search for .bak, .backup, .old
find / -name "*.bak" -o -name "*.backup" -o -name "*.old" 2>/dev/null | xargs du -ch
Core Dumps
# Search for core dumps
find / -name "core.*" -o -name "core" 2>/dev/null
PHP Session Files
du -h /var/lib/php/sessions/
# Clean old sessions
find /var/lib/php/sessions/ -type f -mtime +7 -delete
Increase Disk Space
Add Swap (temporary)
# Create a 2GB swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Make permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Mount External Storage
If your plan includes additional storage:
# List disks
lsblk
# Format (if new)
sudo mkfs.ext4 /dev/sdb
# Create mount point
sudo mkdir /mnt/storage
# Mount
sudo mount /dev/sdb /mnt/storage
# Auto-mount at boot
echo '/dev/sdb /mnt/storage ext4 defaults 0 2' | sudo tee -a /etc/fstab
Automatic Cleanup Script
sudo nano /usr/local/bin/clean-disk.sh
#!/bin/bash
echo "=== Disk cleanup $(date) ==="
# Space before
echo "Space before:"
df -h /
# APT
echo "Cleaning APT..."
apt autoremove -y > /dev/null
apt clean > /dev/null
# Systemd journals
echo "Cleaning journals..."
journalctl --vacuum-time=7d > /dev/null
# Old logs
echo "Cleaning old logs..."
find /var/log -name "*.gz" -delete
find /var/log -name "*.1" -delete
# Temporary files
echo "Cleaning temporary files..."
rm -rf /tmp/* 2>/dev/null
rm -rf /var/tmp/* 2>/dev/null
# Docker (if installed)
if command -v docker &> /dev/null; then
echo "Cleaning Docker..."
docker system prune -af > /dev/null 2>&1
fi
# Space after
echo ""
echo "Space after:"
df -h /
echo ""
echo "=== Cleanup complete ==="
sudo chmod +x /usr/local/bin/clean-disk.sh
Add to cron (weekly):
sudo crontab -e
0 3 * * 0 /usr/local/bin/clean-disk.sh >> /var/log/clean-disk.log 2>&1
Disk Space Alerts
Alert script:
sudo nano /usr/local/bin/disk-alert.sh
#!/bin/bash
THRESHOLD=85
EMAIL="admin@yourdomain.com"
USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ $USAGE -gt $THRESHOLD ]; then
echo "ALERT: Disk at ${USAGE}% on $(hostname)" | \
mail -s "Disk alert $(hostname)" $EMAIL
fi
sudo chmod +x /usr/local/bin/disk-alert.sh
Daily cron:
0 9 * * * /usr/local/bin/disk-alert.sh
Best Practices
- Regularly monitor disk space with
df -h - Configure alerts before reaching 90%
- Automate cleanup with crons
- Limit log size with logrotate
- Store backups elsewhere than on the main VPS
Tip
A disk should never exceed 85% usage to maintain a safety margin.