Sub topic 10
Freeing Space Without Breaking Services
Start with evidence
Assume you can use a Linux shell over SSH, understand files and permissions, and can use sudo. Before deleting anything, identify the full filesystem and the process or service that owns the data. A nearly full filesystem can cause failed writes, but hurried cleanup can also remove active databases, queues, or configuration files.
Use df to confirm the affected mount:
df -hT /var
df -ih /varThen measure directories without crossing into another filesystem:
sudo du -xhd1 /var 2>/dev/null | sort -hThe -x option matters. Without it, a mounted filesystem below /var can make the result look larger than the filesystem you are trying to repair.
Clean logs using their owners
Logs are normally managed by logrotate or by the systemd journal. First inspect the log directory and the rotation policy.
sudo du -xhd1 /var/log 2>/dev/null | sort -h
sudo logrotate -d /etc/logrotate.confThe -d option performs a dry run. It shows what logrotate would do without changing files. Prefer the normal scheduled rotation. If rotation is urgently needed and you have reviewed the configuration, force it:
sudo logrotate -f /etc/logrotate.confFor systemd-managed logs, check their size and remove only old archived entries:
journalctl --disk-usage
sudo journalctl --vacuum-size=500MVacuuming removes archived journal files until their total size is about 500 MB. It does not remove the journal file currently receiving new messages. Configure a permanent limit in /etc/systemd/journald.conf, such as SystemMaxUse=500M, then reload journald according to your distribution's operating procedure.
Check for deleted files still using space
If df reports high usage but du cannot account for it, look for deleted files held open by processes:
sudo lsof -nP +L1The process still owns the file descriptor, so its blocks remain allocated. Record the process and service name. Restart the owning service during an approved maintenance window. Afterward, verify both measurements:
df -hT
sudo du -xhd1 /var 2>/dev/null | sort -hAvoid deleting files through /proc or killing an unknown process. The restart may interrupt requests, drop connections, or discard temporary state.
Remove other data cautiously
Use a print-only command before removing old temporary files:
sudo find /var/tmp -xdev -type f -mtime +14 -printReview the list and remove only files that your system's policy allows. Package caches are another possible target, but use the package manager rather than deleting its database:
# Debian or Ubuntu
sudo apt-get clean
# Fedora, RHEL, or compatible systems
sudo dnf clean all| Target | Safer method | Main risk |
|---|---|---|
| Systemd journal | `journalctl --vacuum-size=500M` | Losing older diagnostic history |
| Rotated logs | Review logrotate configuration | Removing audit or compliance records |
| Package cache | Package manager cleanup | Slower future package installation |
| Application cache | Application documentation | Deleting needed session or queue data |
Freeing space does not always solve inode exhaustion. Millions of small cache, mail-queue, or session files require deleting entries, not just large files. Remove them through the owning service or application procedure.
Exercise
du shows 60 GB under /var, but df says 85 GB is used. What should you check before deleting more files?
Show answer
Run sudo lsof -nP +L1 and investigate deleted files still held open by processes. Restart the owning service during an approved maintenance window, then verify the measurements again.