Step 51 of 82
← Home
πŸ” Phase: Hardening & Access

Step 51 Clear All Log Files

Why this step? Manage log file growth to keep your server clean and performing well.

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.

  1. Edit configuration: /etc/logrotate.conf

  2. Ensure you have settings like:

    weekly
    rotate 4
    compress
  3. 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:

  1. Clean Apt Cache:

    sudo apt clean
  2. Remove Old Kernels:

    sudo apt autoremove --purge
  3. Clear Thumbnail Cache:

    rm -rf ~/.cache/thumbnails/*

Windows 11/10 (Automated Method)

For Windows systems, manual log clearing can be tedious. Instead, use the Cleanup 11 (System Cleaner) tool provided by ComputerBas:

  1. Download Cleanup 11 (clearlog.exe) from the Software Downloads page.
  2. Run the executable as Administrator.
  3. The tool will safely automatically clean up temporary files, Windows update caches, and system logs without breaking any active services or programs.

Manual Method (PowerShell)

If you prefer to clear the Windows Event Logs manually via the command line, open PowerShell as Administrator and run the following command to clear all event logs:

wevtutil el | Foreach-Object {wevtutil cl "$_"}