Free Up Disk Space on Linux by Clearing Logs
In this article
Log files are essential for troubleshooting, but on a busy Linux server they can quietly consume gigabytes of disk space until the root filesystem fills up. This guide shows you how to safely identify large log files, truncate or rotate them, and set up automatic cleanup so you reclaim disk space without losing the logs you actually need.
What You'll Need
- Root or sudo access to the server
- SSH access to the command line
- Basic familiarity with the terminal and common commands like
ls,du, andjournalctl
Step-by-Step
- Check overall disk usage. Run
df -hto see which filesystem is full. Look for a mount point like/or/varwith 100% or near-100% usage. Note the filesystem that needs attention. - Find the biggest log files. Use
sudo du -ah /var/log 2>/dev/null | sort -rh | head -20to list the largest files under/var/log. This shows you exactly which logs are eating space. You can also runsudo du -sh /var/log/*to see totals per directory. - Check systemd journal size. If your server uses systemd (most modern distros do), the journal can grow large. Run
journalctl --disk-usageto see how much space it occupies. - Truncate a specific log file. If you have a large log like
/var/log/syslogthat you no longer need, empty it in place withsudo truncate -s 0 /var/log/syslog. This frees the space immediately without deleting the file, so services that keep the file open continue to work. - Rotate logs instead of deleting. Use
sudo logrotate -f /etc/logrotate.confto force a rotation. This compresses and archives old logs according to the configuration, then starts fresh files. Check the config in/etc/logrotate.d/to adjust retention. - Limit the systemd journal. Run
sudo journalctl --vacuum-size=200Mto shrink the journal to 200 MB, orsudo journalctl --vacuum-time=7dto keep only the last 7 days. To make this permanent, edit/etc/systemd/journald.confand setSystemMaxUse=200M, then restart the service withsudo systemctl restart systemd-journald. - Clean old rotated logs. Logrotate usually keeps a few compressed versions (e.g.,
syslog.1.gz). You can delete older ones manually withsudo rm /var/log/syslog.2.gzif you need space immediately, but be careful not to delete the active file. - Verify space is freed. Run
df -hagain to confirm the filesystem now has free space. Also runsudo du -sh /var/logto see the new total size. - Set up automatic cleanup. Ensure logrotate runs daily via cron (it usually does by default at
/etc/cron.daily/logrotate). For the journal, theSystemMaxUsesetting you added will keep it capped. If you need more aggressive cleanup, add a cron job like0 2 * * * journalctl --vacuum-size=200M.
Common Mistakes
- Deleting the active log file. If you
rma log that a service is writing to, the service keeps writing to the now-unlinked inode, and the space is not freed until you restart the service. Always usetruncateinstead. - Ignoring other large files. Logs are not the only space hogs. Check
/tmp, package caches (/var/cache/apton Debian/Ubuntu), and old kernels. Usesudo du -x -h --max-depth=1 /to find other culprits. - Setting journal limits too low. A 10 MB journal may cause important logs to be dropped. Start with 200 MB or 500 MB and adjust based on your needs.
- Forgetting to test logrotate. A misconfigured logrotate can break services. Always test with
sudo logrotate -d /etc/logrotate.conf(dry run) before forcing a rotation.
Tips
- Use
sudo ls -lh /var/log/*.logto quickly see sizes in human-readable format. - If you have a log that grows extremely fast (e.g., a debug log), consider reducing the application's log level instead of just cleaning up after it.
- Compress old logs with
gzipif your logrotate config doesn't already. This can reduce size by 80-90%. - Monitor disk usage proactively with a simple script that alerts you when
dfshows over 90% usage. - For Docker servers, check container logs in
/var/lib/docker/containers— they can be huge and are often overlooked. Usesudo du -sh /var/lib/docker/containersto check.
Troubleshooting
Disk space still full after clearing logs. Run sudo lsof +L1 to list deleted files that are still open. If you see a process holding a deleted log, restart that service (e.g., sudo systemctl restart rsyslog) to release the space.
logrotate fails with an error. Check the syntax of your config with sudo logrotate -d /etc/logrotate.conf. Common issues include missing directories or invalid rotation counts. Fix the config and run sudo logrotate -f again.
journalctl --vacuum-size doesn't shrink the journal. Ensure you are running it as root (use sudo). If the journal is on a separate filesystem, the vacuum may not work as expected — check with journalctl --disk-usage after the command.
Services stop logging after truncation. This usually happens if you deleted the file instead of truncating. Restart the service to recreate the file, or use sudo touch /var/log/yourlog to create an empty file with the correct ownership.
You accidentally deleted a log you needed. If you have backups, restore from there. Otherwise, check if the log is rotated (e.g., .1 or .gz files) and see if the information is available there. Going forward, consider using a centralized logging service to avoid losing logs.
Frequently Asked Questions
Q: Is it safe to delete all logs in /var/log?
A: No. Some logs are needed for security auditing and troubleshooting. Keep at least recent logs and rotate older ones. Deleting everything can cause services to behave oddly or lose critical information.
Q: What is the difference between truncating and deleting a log file?
A: Truncating (truncate -s 0) empties the file but keeps the inode, so services writing to it continue seamlessly. Deleting (rm) removes the file entry; the space is only freed when the process closes the file descriptor, which may not happen until restart.
Q: How often should I clear logs?
A: It depends on your log volume. For busy servers, daily logrotate is standard. For light usage, weekly or monthly is fine. The key is to have a rotation policy that keeps logs for a useful period (e.g., 30 days) without filling the disk.
Q: Can I clear logs without root access?
A: Usually no, because log files are owned by root. If you have sudo, you can. If not, ask your administrator to set up logrotate or grant you access to specific logs.
Q: Will clearing logs affect my application's performance?
A: No, clearing old logs does not affect running services. Truncating an active log is safe and does not require a restart. Performance may even improve slightly if the disk was nearly full.
Q: How do I check which log file is the largest?
A: Use sudo du -ah /var/log 2>/dev/null | sort -rh | head -20 to list the top 20 largest files. This gives you a clear picture of where space is being used.