How to Clear Logs and Temporary Files in Linux Safely and Free Disk Space

Managing logs and temporary files is an important part of maintaining a healthy Linux system. Over time, these files accumulate, consuming valuable disk space, slowing down performance, and potentially exposing sensitive information. This post explains the importance of clearing logs and temporary files, highlighting how it enhances system performance, improves security, and ensures compliance with data retention policies. It provides a detailed guide on using essential Linux commands to clear, archive, and automate the management of log files and temporary directories like /var/log and /tmp. Additionally, the blog shares best practices for monitoring disk usage, automating cleanup with cron jobs, and using tools like logrotate and tmpreaper. With these strategies, you can optimize your Linux environment for efficiency, security, and long-term reliability.

Dec 02, 2024 - 15:34
Updated: 13 days ago
104.2k
How to Clear Logs and Temporary Files in Linux Safely and Free Disk Space

Quick answer: To free disk space safely, first find what is large with df -h and du -sh, then clean selectively: trim the systemd journal with journalctl --vacuum-size, rely on logrotate for /var/log, truncate a runaway log with truncate -s 0, clear package caches and old temp files. Do not delete logs you may need for troubleshooting or audit.

Key takeaways

  • Diagnose first with df -h and du; do not delete blindly.
  • journalctl --vacuum-size and --vacuum-time trim the systemd journal safely.
  • Truncate an active log with truncate -s 0 instead of deleting it, so the service keeps writing.
  • Logs are evidence; deleting them to hide activity is improper and may be illegal.
  • A deleted file held open by a process still uses space until the process restarts.

How do you find what is using the disk?

Do not start by deleting. First locate the space:

df -h
sudo du -xh /var --max-depth=2 | sort -h | tail -15

df -h shows which file system is full. du shows which directories are largest, and -x stops it crossing into other file systems. On a typical server, the heavy directories are /var/log, /var/cache, /var/lib (databases, containers) and /home. Also check inodes with df -i if the disk seems free but you cannot create files.

How do you clean systemd journal logs?

Modern distributions based on systemd store logs in the journal. Check its size and trim it:

journalctl --disk-usage
sudo journalctl --vacuum-size=500M
sudo journalctl --vacuum-time=14d

To limit it permanently, set SystemMaxUse=500M in /etc/systemd/journald.conf and restart systemd-journald.

What about files in /var/log?

Most services write to /var/log, and logrotate compresses and removes old copies automatically. Check its configuration in /etc/logrotate.conf and /etc/logrotate.d/. To free space from a runaway file without breaking the service:

sudo truncate -s 0 /var/log/app/big.log

Truncating keeps the file and its permissions, and the program keeps writing to it. Using rm on an open log can leave the space still in use (see below). Better still, fix the cause of the flood, such as debug logging left on.

How do you clean package caches and temporary files?

TaskCommand
Clean dnf cache (RHEL, Rocky, AlmaLinux)sudo dnf clean all
Remove unused packages (RHEL family)sudo dnf autoremove
Clean apt cache (Debian, Ubuntu)sudo apt clean and sudo apt autoremove
Old temp filessudo find /tmp -type f -mtime +7 -ls to review, then add -delete
Systemd-managed temp cleanupsystemd-tmpfiles --clean
Old container datapodman system prune or docker system prune after reading what it will remove

Always run the listing form of a find command first, and read the output before adding -delete. CentOS Linux is end-of-life; the same commands apply on supported rebuilds such as Rocky Linux and AlmaLinux.

Why is the disk still full after deleting a file?

If a process has a file open when you delete it, the space is not released until the process closes it. Find such files with:

sudo lsof +L1

Restart the owning service, or truncate the file through /proc/PID/fd if you cannot restart it. This is a classic cause of a disk that stays full after cleaning.

Should you ever clear logs completely?

Logs are evidence for troubleshooting, security investigations and compliance. Keep them for the period your organisation requires, ship them to a central log server, and delete only with a policy. Wiping logs to hide activity is improper and may be a crime in many circumstances, including tampering with evidence under Indian law.

Common mistakes

  • Running rm -rf on /var/log or /tmp without checking what runs from them.
  • Deleting an active log instead of truncating it.
  • Cleaning when the real cause, a chatty application, remains.
  • Forgetting that /tmp may hold files that running programs need.

Next steps

Practise these tasks with the Linux training course. Related reading: essential Linux commands for admin interviews and the most important Linux commands.

Frequently Asked Questions

First find what is large with df and du. Then trim the systemd journal with journalctl --vacuum-size or --vacuum-time, let logrotate manage /var/log, and truncate runaway files with truncate -s 0 rather than deleting them.

Not blindly. Services may be writing to them, and logs are needed for troubleshooting and audit. Truncate an active file, remove old rotated copies according to policy, and fix whatever is generating excessive logs.

List files older than a set number of days with find, review the output and then delete them. Many systems also clean /tmp automatically through systemd-tmpfiles. Avoid removing files that running programs still use.

You should not clean them manually on a schedule. Configure logrotate and journald size limits so old logs are removed automatically, and keep logs for the period required by your operational and compliance needs.

A process may still hold the deleted file open, so the space is not released. Find it with lsof +L1, then restart the service or truncate the file through its entry in /proc. Also check inodes with df -i.

It removes the oldest archived systemd journal files until the total journal size is below the limit you set, for example 500M. It is a safe way to reclaim space, and you can set a permanent limit in journald.conf.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.