Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Free Up Disk Space on Linux by Clearing Logs

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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, and journalctl

Step-by-Step

  1. Check overall disk usage. Run df -h to see which filesystem is full. Look for a mount point like / or /var with 100% or near-100% usage. Note the filesystem that needs attention.
  2. Find the biggest log files. Use sudo du -ah /var/log 2>/dev/null | sort -rh | head -20 to list the largest files under /var/log. This shows you exactly which logs are eating space. You can also run sudo du -sh /var/log/* to see totals per directory.
  3. Check systemd journal size. If your server uses systemd (most modern distros do), the journal can grow large. Run journalctl --disk-usage to see how much space it occupies.
  4. Truncate a specific log file. If you have a large log like /var/log/syslog that you no longer need, empty it in place with sudo 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.
  5. Rotate logs instead of deleting. Use sudo logrotate -f /etc/logrotate.conf to 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.
  6. Limit the systemd journal. Run sudo journalctl --vacuum-size=200M to shrink the journal to 200 MB, or sudo journalctl --vacuum-time=7d to keep only the last 7 days. To make this permanent, edit /etc/systemd/journald.conf and set SystemMaxUse=200M, then restart the service with sudo systemctl restart systemd-journald.
  7. Clean old rotated logs. Logrotate usually keeps a few compressed versions (e.g., syslog.1.gz). You can delete older ones manually with sudo rm /var/log/syslog.2.gz if you need space immediately, but be careful not to delete the active file.
  8. Verify space is freed. Run df -h again to confirm the filesystem now has free space. Also run sudo du -sh /var/log to see the new total size.
  9. Set up automatic cleanup. Ensure logrotate runs daily via cron (it usually does by default at /etc/cron.daily/logrotate). For the journal, the SystemMaxUse setting you added will keep it capped. If you need more aggressive cleanup, add a cron job like 0 2 * * * journalctl --vacuum-size=200M.

Common Mistakes

  • Deleting the active log file. If you rm a 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 use truncate instead.
  • Ignoring other large files. Logs are not the only space hogs. Check /tmp, package caches (/var/cache/apt on Debian/Ubuntu), and old kernels. Use sudo 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/*.log to 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 gzip if 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 df shows over 90% usage.
  • For Docker servers, check container logs in /var/lib/docker/containers — they can be huge and are often overlooked. Use sudo du -sh /var/lib/docker/containers to 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.