Tips and tricks: Cleanup and analyze Ubuntu server disk space

Ubuntu Disk Cleanup & Package Manager Troubleshooting Guide

This post outlines routine procedures for reclaiming disk space on Ubuntu systems and resolving associated dpkg-statoverride packaging failures.

1. Storage Optimization Commands

Run these commands sequentially to purge system caches, logs, and orphaned dependencies.

# Remove unused software dependencies and configurations
sudo apt-get autoremove --purge -y

# Clear the local repository cache of downloaded package files
sudo apt-get clean

# Query systemd journal log size and vacuum down to 100MB
sudo journalctl --disk-usage
sudo journalctl --vacuum-size=100M

# Purge disabled/historical duplicate Snap package revisions
snap list --all | awk '/disabled/{print $1, $3}' | while read name rev; do sudo snap remove "$name" --revision="$rev"; done

# Flush user space thumbnail caches and desktop trash bins
rm -rf ~/.cache/thumbnails/* ~/.local/share/Trash/*

2. Analyzing Storage Distribution

Install and deploy ncdu (NCurses Disk Usage) to analyze directories via an interactive CLI interface.

sudo apt update && sudo apt install ncdu -y
sudo ncdu /
  • Navigation: Up/Down to browse, Enter to open, Left Arrow to go back.
  • Deletion: Press Shift + D to permanently erase a selected target.

3. Troubleshooting: Dpkg Statoverride Failures

Symptom

APT or DPKG operations halt with an unrecoverable fatal error regarding an unknown system user (e.g., 'redis') in the statoverride file. This occurs when a service user account is deleted while its security file-permission overrides persist.

dpkg: unrecoverable fatal error, aborting:
 unknown system user 'redis' in statoverride file [...]
E: Sub-process /usr/bin/dpkg returned an error code (2)

Resolution

  1. Open the internal dpkg permission override registry:
    sudo nano /var/lib/dpkg/statoverride
    
  2. Locate and delete (Ctrl + K in Nano) the problematic orphan line:
    redis redis 640 /etc/redis/redis.conf
    
  3. Save (Ctrl + O, Enter), exit (Ctrl + X), and force-repair the package database:
    sudo apt-get install -f