Ubuntu Disk Cleanup Guide (Docker + KVM/libvirt)
Last reviewed: 2026-09-17
Purpose: Command-level walkthrough for reclaiming disk space on an Ubuntu host, covering Docker artefacts, KVM/libvirt VM storage, journal logs, caches and the tools that make the investigation quick.
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
For a broader discussion of strategy and risk, see Disk-Cleanup.md.
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 β maximum 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 the VM
Remove unnecessary files.
Step 2: Zero free space
Step 3: Compact the image
Replace the original file after verification.
Find orphaned disks
Delete disks that are 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)
The quickest way to spot large files and directories interactively.
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
8. Important Warnings
- Do not delete
/var/lib/dockermanually - Do not delete
.qcow2files blindly - Always verify before removing VMs
- Use
virshfor VM management
9. Recommended Workflow
- Identify large directories (
du,ncdu) - Check
/var/lib - Investigate Docker and libvirt
- Remove unused VMs or data
- Clean logs and caches
- Prevent future growth (log rotation)
Topics Covered
- Diagnosing disk usage with
duandncdu - Docker image, container, volume and build-cache cleanup, plus log rotation
- KVM/libvirt VM inspection, removal, orphaned-image detection and qcow2 compaction
- Journal and system-log vacuuming, apt and snap cache cleanup
- A safe, ordered cleanup workflow with the operations that must not be done casually