Clear All Log Files
Guide on safely clearing or deleting log files in Linux systems to free up disk space without breaking running services.
The Risks
❌ *Never run `rm -rf /var/log/`* Deleting the files* themselves can break services (like Apache, Nginx, or Syslog) because they still hold the file handle open. They will stop logging until restarted.
✅ Correct Method: Truncate the file content to zero bytes while keeping the file properly in place.
Method 1: The Safe "Truncate" Loop
This command finds all files in /var/log ending in .log (or all files) and empties them.
sudo find /var/log -type f -name "*.log" -exec truncate -s 0 {} \;
truncate -s 0: Sets file size to 0 bytes.
Method 2: Systemd Journal (journalctl)
On modern systemd systems (Ubuntu 16.04+, Debian 8+), most logs are stored in the journal binary format.
Check Disk Usage
journalctl --disk-usage
Vacuum (Clear) Old Logs
Retain only the last 100MB:
sudo journalctl --vacuum-size=100M
Retain only the last 2 days:
sudo journalctl --vacuum-time=2d
Method 3: Logrotate (The Proper Way)
Instead of manually deleting logs, configure logrotate to do it automatically.
-
Edit configuration:
/etc/logrotate.conf -
Ensure you have settings like:
weekly rotate 4 compress -
Force a rotation now:
sudo logrotate -f /etc/logrotate.conf
Method 4: Removing Old Archives
If you have many access.log.1.gz, syslog.2.gz, etc., you can safely delete these compressed archives as they are connected to nothing.
# Delete all gzipped log archives
sudo find /var/log -type f -name "*.gz" -delete
Emergency Space Recovery
If your disk is 100% full:
-
Clean Apt Cache:
sudo apt clean -
Remove Old Kernels:
sudo apt autoremove --purge -
Clear Thumbnail Cache:
rm -rf ~/.cache/thumbnails/*