Skip to content

๐Ÿงน 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

sudo du -h / --max-depth=1 | sort -hr

Drill into /var

sudo du -h /var --max-depth=1 | sort -hr

Drill into /var/lib

sudo du -h /var/lib --max-depth=1 | sort -hr

๐Ÿณ 2. Docker Cleanup

Check Docker size

sudo du -sh /var/lib/docker

Clean unused resources

sudo docker system prune -a -f

Clear container logs

sudo truncate -s 0 /var/lib/docker/containers/*/*.log

Limit future log growth

Edit:

sudo nano /etc/docker/daemon.json

Add:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  }
}

Restart Docker:

sudo systemctl restart docker

๐Ÿ–ฅ๏ธ 3. KVM / libvirt Disk Management

Check VM disk usage

sudo du -h /var/lib/libvirt/images | sort -hr | head -20

List virtual machines

virsh list --all

๐Ÿงน Delete unused VM (frees space immediately)

virsh undefine <vm-name> --remove-all-storage

โš ๏ธ This permanently deletes the VM and its disk.


๐Ÿ” Inspect disk usage (important)

qemu-img info /var/lib/libvirt/images/<file>.qcow2

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:

df -h
du -h / --max-depth=1 | sort -hr

Remove unnecessary files.


Step 2: Zero free space

dd if=/dev/zero of=zero.fill bs=1M
rm zero.fill

Step 3: Compact image

qemu-img convert -O qcow2 old.qcow2 compacted.qcow2

Replace original file after verification.


๐Ÿงช Find orphaned disks

ls /var/lib/libvirt/images
virsh list --all

๐Ÿ‘‰ Delete disks not attached to any VM (after verification).


๐Ÿ“ฆ 4. System Logs Cleanup

Check journal size

journalctl --disk-usage

Clean logs (time-based)

sudo journalctl --vacuum-time=7d

Clean logs (size-based)

sudo journalctl --vacuum-size=500M

๐Ÿ“ 5. General Cleanup Targets

Logs

sudo du -sh /var/log

Cache

sudo du -sh /var/cache

Clean:

sudo apt clean

Snap cleanup

snap list --all

Remove old revisions:

sudo snap remove <package> --revision=<old_revision>

๐Ÿ”ง 6. Useful Tools

ncdu (interactive disk analyzer)

sudo apt install ncdu
sudo ncdu /

๐Ÿ‘‰ 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/docker manually

  • โŒ Do NOT delete .qcow2 files blindly

  • โŒ Always verify before removing VMs

  • โœ… Use virsh for 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

  1. Identify large directories (du, ncdu)

  2. Check /var/lib

  3. Investigate:

  4. Docker

  5. libvirt

  6. Remove unused VMs or data

  7. Clean logs and caches

  8. Prevent future growth (log rotation)


End of Guide