Introduction / Why This Is Needed
Disk space on Linux servers and workstations often runs out due to the accumulation of temporary files, package caches, old logs, and unnecessary packages. This guide will help you quickly diagnose which data occupies the most space and safely free up gigabytes without losing important information. You will learn how to use standard utilities (df, du, find) and package managers to keep your system clean.
Prerequisites / Preparation
Before you begin, ensure that:
- You have access to a Linux terminal with sudo (administrator) privileges to execute removal and analysis commands on system directories.
- Basic utilities are installed:
df,du,find,sort,grep. They are available in any standard Linux distribution. - For easier analysis, it is recommended to install
ncdu(sudo apt install ncduorsudo dnf install ncdu), but this is optional. - Caution: Do not delete files in system directories (
/bin,/sbin,/usr,/etc) unless you are certain of their purpose.
Step-by-Step Guide
Step 1: Check Current Disk Usage
First, determine which disk partitions are full. The df (disk free) command shows the usage of each mounted partition.
df -h
Flags:
-h— human-readable, displays sizes in MB, GB.- Output:
Filesystem Size Used Avail Use% Mounted on. Pay attention toUse%— if the value is close to 100%, this is critical.
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 48G 2.0G 96% /
In this case, the root partition / is 96% full. Further actions will focus on it.
Step 2: Find the Largest Directories
Now you need to find which directories occupy the most space. Start from the root /.
sudo du -h --max-depth=1 / | sort -rh | head -n 20
Explanation:
sudo— required to access some system directories.du -h— calculates disk usage by directories.--max-depth=1— shows only the first level of subdirectories (only root subdirectories).sort -rh— sorts by descending size (human-readable numeric).head -n 20— outputs the top 20 largest.
If du runs slowly on a large partition, use ncdu (an interactive tool):
sudo ncdu /
It allows you to navigate through directories and see sizes in real time.
Step 3: Clean Package Manager Cache
Package managers store downloaded package archives in a cache. Over the years, they can accumulate several gigabytes.
For Ubuntu/Debian (apt):
sudo apt clean
Deletes all files from /var/cache/apt/archives. Safe, as packages are already installed.
For Fedora/CentOS/RHEL (dnf/yum):
sudo dnf clean all
or for older versions:
sudo yum clean all
Step 4: Remove Old Logs
Logs in /var/log can grow large, especially if there are system errors. Clean up old or compressed logs.
Caution: Do not delete files that are actively being used (e.g., syslog, auth.log without compression). It is better to delete only archives (*.gz, *.old) and logs older than a certain period.
# Delete compressed logs older than 30 days
sudo find /var/log -type f -name "*.gz" -mtime +30 -delete
# Delete regular (uncompressed) logs older than 7 days (use with caution!)
sudo find /var/log -type f -name "*.log" -mtime +7 -delete
Also check the size of specific logs:
sudo du -sh /var/log/*
If you see huge files (e.g., kern.log), you might need to configure logrotate (usually already present).
Step 5: Clean Temporary Files
Temporary directories /tmp and /var/tmp often contain unnecessary data. Clean them, but ensure no important files are present.
# Clean /tmp
sudo rm -rf /tmp/*
# Clean /var/tmp
sudo rm -rf /var/tmp/*
Also check /var/cache — application caches may be there:
sudo du -sh /var/cache/*
If you see large directories (e.g., man, apt, yum), they can be cleaned similarly to Step 3.
Step 6: Remove Unnecessary Packages and Dependencies
Over time, packages installed as dependencies but no longer needed accumulate. Remove them.
For Ubuntu/Debian:
# Show packages that can be removed (dependencies)
sudo apt autoremove --purge
# Also check the list of installed packages and remove unnecessary ones manually:
apt list --installed | less
# Then: sudo apt remove package-name
For Fedora/CentOS:
sudo dnf autoremove
Important: Do not remove packages manually unless you are sure they are not required by the system. The autoremove command is safe.
Verify the Result
After completing all steps, check disk usage again:
df -h
Compare Used and Avail values with the initial ones. You should see an increase in free space (Avail). You can also repeat the directory analysis (Step 2) to confirm.
Possible Issues
- Access Denied Error
- Solution: use
sudofor commands requiring administrator rights. Do not runrm -rfwithout understanding what you are deleting.
- Solution: use
- Space Not Freed After Deleting Files
- Cause: a process still holds an open file descriptor to the deleted file.
- Solution: find the process:
sudo lsof | grep '(deleted)'. Then restart the corresponding process (e.g., a service) or terminate it.
- System Became Unstable After Removing Packages
- Cause: a critical package was accidentally removed.
- Solution: reinstall the package via
sudo apt install package-name(or equivalent). If you have a backup of the package list, you can restore the system.
- Critical Logs Were Deleted
- Solution: if you deleted an active log, restart the service that writes to it (e.g.,
sudo systemctl restart rsyslog) to create a new file. Old data may be lost.
- Solution: if you deleted an active log, restart the service that writes to it (e.g.,
- Not Enough Space Even After Cleanup
- There may be large files not accounted for (e.g., database dumps, virtual machines). Check user home directories and application directories (
/opt,/var/lib/dockerif Docker is used).
- There may be large files not accounted for (e.g., database dumps, virtual machines). Check user home directories and application directories (
Recommendation: Regularly clean package caches and logs (once a month) to maintain system health.