Log files are a crucial element of any system, as they provide a detailed record of events that occur within the system. However, they can eventually occupy a significant portion of storage space, impairing system efficiency. Therefore, it’s important to manage log files and delete old ones to free up space and maintain system performance. As a potential solution, logrotate can rotate and purge log files automatically.
logrotate is a program that controls log records through rotation at specified intervals, compression, and removal of old files. It is a robust utility that can enhance log file management, particularly in systems with high log data creation.
In this tutorial, we’ll discuss using logrotate to delete files.
2. Setting up logrotate for Deletion
Before we delete outdated log files using logrotate, it is necessary to create a logrotate configuration file. Directives and conditions for removing log files, age, and the number of log files, are included in this file.
Let’s check out the procedures to create a logrotate configuration file.
2.1. Creating a logrotate Configuration File for Deleting Logs
To generate a logrotate configuration file, open a text editor and create a new file with the “.conf” extension, such as “logrotate.conf”. The logrotate configuration file comprises partitions, each indicating how to rotate a distinct log file.
An instance of a logrotate configuration file for purging outdated log files is as follows:
This configuration file specifies that logrotate should rotate and delete log files older than 30 days or larger than 100MB. It also keeps a maximum of 7 compressed log files.
In the logrotate configuration file, it is possible to specify the criteria for log deletion. The age of log files and the number of log files retained are significant factors. Additionally, logrotate allows for the specification of the maximum age of log files to delete by utilizing the “maxage” option. This option determines the max-age of a log file days before deletion. Using the logrotate configuration file, one can set “maxage 30” to remove logs older than 30 days.
We can also specify the number of log files to maintain using the “rotate” option, defining the number of log files to retain, including the latest one. To demonstrate, if we want to store up to 7 compressed log files, we can set “rotate 7” in the logrotate configuration file.
Apart from the “maxage” and “rotate” options, we can leverage the “size” option to define the largest log file size in bytes that logrotate should delete. This option guarantees that log files don’t consume too much disk space. To illustrate, if we have log files exceeding 100MB, we can set “size 100M” in the logrotate configuration file to delete it.
3. Using logrotate to Delete Old Log Files
By creating a logrotate configuration file and defining the criteria for log deletion, we can automate log deletion and maintain efficient system functionality. With the logrotate configuration file set up, we can now delete old log files. Primarily there are two ways to use logrotate for log deletion.
3.1. Running logrotate Manually to Delete Logs
We can run logrotate manually to delete old log files. To do this, we use the following command:
$ logrotate /etc/conf/logrotate.conf
This command runs logrotate with the specified configuration file and rotates and deletes old log files according to the rules specified in the configuration file.
3.2. Setting up a Cron Job for Automatic Log Deletion
We can streamline the log deletion process through a cron job, which automatically runs logrotate at specified intervals. A cron job is a predetermined task that executes automatically at predetermined intervals. To set up a cron job for logrotate, we must add a new entry to the crontab file. The crontab file contains a list of scheduled tasks for the system.
Let’s look at the command to edit the crontab file:
$ crontab -e
Executing this command opens the crontab file in the default text editor. Subsequently, we can include a new entry to implement logrotate at the desired interval, whether daily or weekly. As an illustration, to activate logrotate daily at midnight, we can include the following entry:
$ 0 0 * * * logrotate /etc/conf/logrotate.conf
This entry runs logrotate with the specified configuration file every day at midnight.
3.3. Testing logrotate Configuration to Ensure Proper Deletion
Before implementing logrotate for log deletion, it’s essential to test the logrotate configuration to ensure that it works correctly. We can test the configuration by running logrotate manually with the “-d” option, which simulates log rotation and deletion without actually deleting any files:
$ logrotate -d /etc/conf/logrotate.conf
The execution of this command triggers logrotate with the specified configuration file. It generates a report of the files that would be rotated and deleted without actually deleting any files. We can review the list to confirm that logrotate follows the rules defined in the configuration file for the correct file deletion.
In this article, we discussed how to delete files using the logrotate utility.
First, we looked at the configuration tothe log files. After that, we examined manual deletion and cron jobs for deleting log files.