As of my last update in 2023, systemd and its journaling system, journald, do not provide a built-in feature to selectively delete logs for a specific service beyond managing the overall size and age of the logs in the journal. The journal is stored in a binary format, and logs from different services are interleaved.
The primary tool for managing the size and retention of logs is journalctl, which allows for vacuuming based on time (--vacuum-time=) and size (--vacuum-size=), but these operations affect the journal as a whole, not entries for specific services.
If you need to delete logs for a specific service from before today (or any other point in time), you have a few options, though none are as straightforward as a single command:
Managing Logs with System Settings
Adjust journald settings to manage log retention more broadly, though this affects all services, not just one:
- By Size: Limit the maximum disk space the journal can consume.
SystemMaxUse=500M
- By Time: Configure how long to keep log entries.
MaxRetentionSec=1week
These options need to be added to /etc/systemd/journald.conf and require a restart of systemd-journald:
sudo systemctl restart systemd-journald
Manual Extraction and Deletion
For a service-specific operation, you would have to manually extract the logs you wish to keep, clear the journal, and then potentially repopulate it with the extracted logs. However, this is complex and risks the integrity and security of your logs.
Forwarding Logs and Managing Externally
Another approach to consider is forwarding logs to an external system or file, where you can manage them more flexibly. This could be done via rsyslog, syslog-ng, or a similar logging system that can separate logs by service and manage retention more granularly.
Scripting a Solution
A theoretically possible (but technically challenging and not recommended) approach is to script the extraction of all logs except those you want to delete, clear the journal, and then reinsert the logs. However, this involves risks of data loss, corruption of the log system, and potential security implications. Additionally, inserting logs back into the journald system isn't straightforward due to its binary format and how it handles indexing and timestamps.
Conclusion
For auditability, traceability, and security reasons, it's wise to carefully consider the implications of deleting logs. If your goal is to manage disk space more efficiently, adjusting journald's system-wide settings for retention based on size or time might be the most straightforward and safe approach. For more specific log management needs, forwarding logs to a dedicated system or service designed for log management could offer the flexibility and features you need without compromising the integrity of your system's logs.