๐งน Ubuntu Disk Cleanup Guide (Docker + KVM/libvirt)
๐ Overview
This guide helps you quickly identify and clean up disk space on an Ubuntu system, focusing on:
-
Docker
-
KVM / libvirt virtual machines
-
System logs
๐ 1. Identify Disk Usage
Check top-level disk usage
Drill into /var
Drill into /var/lib
๐ณ 2. Docker Cleanup
Check Docker size
Clean unused resources
Clear container logs
Limit future log growth
Edit:
Add:
Restart Docker:
๐ฅ๏ธ 3. KVM / libvirt Disk Management
Check VM disk usage
List virtual machines
๐งน Delete unused VM (frees space immediately)
โ ๏ธ This permanently deletes the VM and its disk.
๐ Inspect disk usage (important)
Key fields:
-
virtual size โ max allocated size
-
disk size โ actual used space
Interpretation:
| Case | Meaning |
|---|---|
| disk size โ virtual size | Disk is full (no easy recovery) |
| disk size << virtual size | Can reclaim space |
๐ง Compact a VM disk
Step 1: Clean inside VM
Inside the VM:
Remove unnecessary files.
Step 2: Zero free space
Step 3: Compact image
Replace original file after verification.
๐งช Find orphaned disks
๐ Delete disks not attached to any VM (after verification).
๐ฆ 4. System Logs Cleanup
Check journal size
Clean logs (time-based)
Clean logs (size-based)
๐ 5. General Cleanup Targets
Logs
Cache
Clean:
Snap cleanup
Remove old revisions:
๐ง 6. Useful Tools
ncdu (interactive disk analyzer)
๐ Best tool for quickly spotting large files and directories.
๐ 7. Quick Cleanup Script
# Docker logs
sudo truncate -s 0 /var/lib/docker/containers/*/*.log
# Docker unused data
sudo docker system prune -a -f
# System logs
sudo journalctl --vacuum-time=7d
โ ๏ธ Important Warnings
-
โ Do NOT delete
/var/lib/dockermanually -
โ Do NOT delete
.qcow2files blindly -
โ Always verify before removing VMs
-
โ Use
virshfor VM management
๐ก Key Takeaways
-
Large
/varโ usually Docker or VM disks -
Large
/var/lib/libvirtโ VM storage (real data) -
Logs are rarely the main issue at TB scale
-
Biggest gains come from:
-
deleting unused VMs
-
cleaning inside VMs
-
pruning Docker
โ Recommended Workflow
-
Identify large directories (
du,ncdu) -
Check
/var/lib -
Investigate:
-
Docker
-
libvirt
-
Remove unused VMs or data
-
Clean logs and caches
-
Prevent future growth (log rotation)
End of Guide