Clearing Unnecessary Logs on Ubuntu

Logs are crucial for diagnosing issues and monitoring the health of your system. However, over time, logs can accumulate and consume valuable disk space. Here's a step-by-step guide on how to clear unnecessary logs on Ubuntu, while ensuring you retain important system data.

1. Check Log Directories

Most system logs are stored in the /var/log/ directory. Start by listing the contents to get an overview of which logs are present:

ls /var/log/

2. View Log Sizes

Identify which log files are taking up the most space by using the du command:

du -h /var/log/

This will display the size of each log file in a human-readable format.

3. Remove Old Log Files

You can delete specific log files that you no longer need. For example, to remove an old syslog file:

sudo rm /var/log/syslog.1

Replace syslog.1 with the name of the log file you wish to delete. Always check that the log is safe to remove before proceeding.

4. Rotate Logs Automatically

Instead of manually deleting logs, use log rotation to manage file sizes and archive older logs automatically. The logrotate utility handles this:

  • Logrotate configuration files are located in /etc/logrotate.conf and /etc/logrotate.d/.

To trigger log rotation manually:

sudo logrotate -f /etc/logrotate.conf

5. Clean Package Manager Logs

The package manager stores logs that track installed and removed packages. To clean these logs, use:

sudo rm /var/log/apt/*.log

6. Clear Systemd Journal Logs

If your system uses systemd, logs are managed by journald. To clear old systemd logs, run:

sudo journalctl --vacuum-time=7d

This command will remove journal logs older than 7 days. Adjust the time parameter as needed.

7. Delete Crash Logs

Crash logs are stored in /var/crash/ and are useful for debugging. To view crash logs:

ls /var/crash/

To remove a specific crash log:

sudo rm /var/crash/example.crash

To remove all crash logs:

sudo rm /var/crash/*

8. Remove All .log and .gz Files

To delete all log files with .log or .gz extensions inside the /var/log/ directory, use:

sudo find /var/log -type f \( -name "*.log" -o -name "*.gz" \) -exec rm {} +

9. Remove Logs with Specific Patterns (e.g., .log.1, .log.2)

To also delete log files like syslog.1 or auth.log.1, modify the find command:

sudo find /var/log -type f \( -name "*.log" -o -name "*.gz" -o -name "*.log.[0-9]" \) -exec rm {} +

10. Reboot the System

After cleaning logs, it's good practice to reboot the system to ensure services restart and fresh log files are generated:

sudo reboot

Important Notes

  • Backup Logs: Before deleting, consider backing up important logs for future troubleshooting.
  • Avoid Deleting Critical Logs: Be cautious when removing logs that are essential for security and system diagnostics, such as auth.log, syslog, and kern.log.

By following this tutorial, you can free up disk space on your Ubuntu system while maintaining critical logs needed for system health monitoring.

Continue Reading

Discover more amazing content handpicked just for you

Tutorial

How to Stop SSH From Timing Out

Restart SSH:

sudo systemctl restart sshd
Aug 21, 2025
Read More
Tutorial

✅ How to Disable MySQL Password Validation on Ubuntu 25.04

If disabled, this will return an empty result set.

Now that validation is disabled, try creating a user with a weak password:

May 01, 2025
Read More
Tutorial

How to Move the MySQL Data Directory to a New Location on Ubuntu 25.04

Find the line:

datadir = /var/lib/mysql
May 01, 2025
Read More
Tutorial

How to Install PHP, MySQL, and phpMyAdmin on Ubuntu 25.04 (LAMP Stack Setup Guide)

  • When prompted to choose a web server, select apache2.
  • Choose Yes when asked to configure the database for phpMyAdmin with dbconfig-common.
  • Set a password for the phpMyAdmin application.

Enable the mbstring PHP extension and restart Apache:

May 01, 2025
Read More
Tutorial

How to Fix NVIDIA Driver Issues on Ubuntu (Dell Vostro 3521)

nvidia-smi

You should see output like:

Apr 14, 2025
Read More
Tutorial

🛡️ Protect Your Forms Like a Pro: Anti-Spam Techniques That Actually Work

Spam submissions are one of the most common annoyances for web developers. Whether you're dealing with contact forms, login pages, or comment sections—bots will find and abuse them.

In this tutorial, you'll learn real-world, effective anti-spam techniques beyond just slapping on a CAPTCHA. These strategies are easy to implement, and when combined, they make your forms extremely hard to abuse.

Apr 04, 2025
Read More
Tutorial
bash

Setting Correct Permissions for Laravel

sudo chown -R www-data:www-data /path/to/your/laravel_project/storage
sudo chown -R www-data:www-data /path/to/your/laravel_project/bootstrap/cache

Replace /path/to/your/laravel_project with the path to your Laravel application.

Nov 07, 2024
Read More
Tutorial
bash

Renaming a subdomain in Apache and generating a new SSL certificate

Since the SSL certificate is tied to the specific domain name, you’ll need to create a new one for new.example.com. Certbot makes this easy.

Run the following command:

Nov 07, 2024
Read More
Tutorial

How To view tables and the number of records in each table in a PostgreSQL database,

These methods will help you view tables and get a record count for each in your PostgreSQL database.

Nov 06, 2024
Read More
Tutorial

How to install PostgreSQL

   sudo apt install postgresql postgresql-contrib

PostgreSQL starts automatically; to check the status:

Nov 06, 2024
Read More
Tutorial

How to view tables on a PostgreSQL database hosted on Heroku

After connecting, you can explore the database tables in the client’s interface. Most clients allow you to view tables directly and run SQL queries to list tables with:

   SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
Nov 06, 2024
Read More
Tutorial

How to view your Heroku Apps

Sign in to your Heroku account if you’re not already logged in.

Once logged in, you’ll see a list of all your applications on the dashboard. You can click on each app to view details, manage settings, view metrics, and more.

Nov 06, 2024
Read More
Cheatsheet

Essential dpkg Commands Cheat Sheet for Debian and Ubuntu Systems

  • What it is: A tool for installing, building, removing, and managing Debian packages.
  • Usage: Primarily used through the command line with different action parameters.
  • Primary Manager: The default package manager for Debian and Debian-based systems, including Ubuntu.
  • Install a package:
Oct 24, 2024
Read More
Tutorial

Getting Started with DevSecOps — Secure CI/CD Pipelines with Jenkins

If your team uses Slack, you can integrate Jenkins with Slack to receive real-time notifications about your pipeline status.

Tools like OWASP Dependency-Check and SonarQube generate security reports that can be visualized on the Jenkins dashboard or sent via email.

Oct 22, 2024
Read More
Tutorial
bash

How to Install and Configure Apache on Ubuntu

sudo systemctl enable apache2

Apache logs can help troubleshoot issues with your web server. The logs are located in the /var/log/apache2/ directory.

Oct 21, 2024
Read More
Tutorial
bash

How to Create SSL for a Website on Ubuntu

Securing your website with SSL (Secure Socket Layer) certificates ensures encrypted communication between the server and the users. In this tutorial, you'll learn how to create and install SSL certificates on an Ubuntu-based server using Let's Encrypt and Certbot. Let's Encrypt provides free SSL certificates, and Certbot simplifies the process of obtaining and renewing them.

  • A server running Ubuntu (18.04, 20.04, or newer).
  • A domain name pointing to the server's public IP.
  • Root or sudo user privileges.
  • A web server such as Apache or Nginx installed.
Oct 21, 2024
Read More
Tutorial
bash

How to Install MongoDB Shell (mongosh) on Ubuntu

mongosh mongodb://localhost:27017

This will open the MongoDB shell connected to your local database.

Oct 18, 2024
Read More
Tutorial

How to Install MongoDB on Ubuntu

  use mydatabase
  • Insert a Document:
Oct 18, 2024
Read More
Cheatsheet

PM2 Cheatsheet

  • Save the list of running apps for auto-respawn after system restart.
pm2 resurrect
Oct 14, 2024
Read More
Tutorial
bash

How to Update Node.js and npm on Ubuntu

node -v
npm -v

If the version is outdated (e.g., Node.js v12 or earlier), it's time to upgrade.

Oct 03, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!