YugabyteDB logs are incredibly useful for troubleshooting, performance analysis, auditing, and support investigations.
But like all logs, they need a little housekeeping.
YugabyteDB already rotates many of its log files automatically, but rotation is not the same thing as long-term retention management. Over time, older rotated logs can still consume disk space unless you archive or delete them.
So what is the best way to clean up old YugabyteDB logs?
The short answer: use the operating system, not pg_cron.
🧠 Key Insight
Use pg_cron for scheduled database work. Use Linux cron, systemd timers, logrotate, or your server automation tooling for operating system work like copying, compressing, archiving, or deleting log files.
Log Rotation vs. Log Retention
YugabyteDB nodes can produce several types of logs, including YB-Master logs, YB-TServer logs, YSQL logs, and other diagnostic files.
These logs are important when debugging issues, reviewing cluster behavior, or working with support. However, if old logs are never cleaned up, they can slowly consume disk space.
That makes log retention an operational task worth planning.
A good log retention strategy usually answers three questions:
1. How long should logs stay on the database nodes?
2. Should older logs be archived somewhere else?
3. When is it safe to delete them locally?
Where YugabyteDB Stores Logs
The exact log location depends on how YugabyteDB is deployed.
For YugabyteDB Anywhere-managed universes, logs are commonly available here:
These directories are typically symlinks to the first directory listed in --fs_data_dirs.
For other deployments, logs are usually under the YugabyteDB data directory. For example:
/master/logs
/tserver/logs
YugabyteDB also has WAL directories, tablet data directories, RocksDB files, and other internal storage paths.
Those are not regular log files.
⚠️ Important
This tip is about cleaning up diagnostic log files only. Do not delete anything under YugabyteDB WAL directories, tablet data directories, RocksDB directories, or DocDB data directories.
Why Not Use pg_cron?
pg_cron is designed for scheduling SQL commands inside the database.
That makes it useful for scheduled database tasks like this:
SELECT cron.schedule(
'daily-table-cleanup',
'0 2 * * *',
$$DELETE FROM app_events WHERE created_at < now() - interval '90 days'$$
);
But archiving or deleting log files is not really a database task.
This is an operating system task:
find /home/yugabyte/tserver/logs -type f -mtime +14 -delete
That kind of work belongs in Linux cron, a systemd timer, logrotate, Ansible, Chef, Puppet, Kubernetes jobs, or whatever automation tool you already use to manage your servers.
Recommended Approach
The safest pattern is:
1. Let YugabyteDB write and rotate its own logs.
2. Use an operating system-level scheduled job to manage retention.
3. Archive older logs if needed.
4. Delete only files from known log directories.
5. Never run cleanup commands against broad YugabyteDB data paths.
For many environments, a simple daily cron job on each database node is enough.
Run the Job on Every DB Node
YugabyteDB log files are stored locally on each YB-Master and YB-TServer node.
That means a log cleanup or archive job needs to run on every database server in the cluster, not just one node.
For example, in a 3-node universe, you would typically install the cron job on all three nodes:
node-1: install /usr/local/bin/cleanup-yb-logs.sh and add the cron entry
node-2: install /usr/local/bin/cleanup-yb-logs.sh and add the cron entry
node-3: install /usr/local/bin/cleanup-yb-logs.sh and add the cron entry
In production, this is usually handled through configuration management or automation tooling such as Ansible, Puppet, Chef, Terraform, cloud-init, or a standard operations runbook.
🧠 Key Insight
YugabyteDB logs are node-local. If you manage retention with cron, install the same cleanup or archive job on every DB node, or use centralized logging so retention is handled outside the database servers.
Option 1: Delete Logs Older Than X Days
Here is a simple example that deletes YugabyteDB log files older than 14 days.
Create a script:
sudo vi /usr/local/bin/cleanup-yb-logs.sh
Add the following:
#!/usr/bin/env bash
set -euo pipefail
RETENTION_DAYS=14
LOG_DIRS=(
"/home/yugabyte/master/logs"
"/home/yugabyte/tserver/logs"
)
for LOG_DIR in "${LOG_DIRS[@]}"; do
if [ ! -d "$LOG_DIR" ]; then
echo "Skipping missing directory: $LOG_DIR"
continue
fi
echo "Cleaning logs older than ${RETENTION_DAYS} days in ${LOG_DIR}"
find "$LOG_DIR" \
-type f \
-mtime +"$RETENTION_DAYS" \
-print \
-delete
done
find "$LOG_DIR" \
-type f \
-mtime +2 \
-print0 | while IFS= read -r -d '' file; do
if command -v lsof >/dev/null 2>&1 && lsof "$file" >/dev/null 2>&1; then
echo "Skipping open file: $file"
continue
fi
aws s3 cp "$file" "s3://my-yb-log-archive/${HOSTNAME}/${COMPONENT}/$(basename "$file")"
done
From there, an object storage lifecycle policy can move older logs to cheaper storage or expire them automatically.
This job also needs to run on each DB node unless logs are already being collected centrally.
What About logrotate?
logrotate can also be useful, especially if your operations team already uses it.
However, YugabyteDB already performs its own log rotation. For example, the --max_log_size flag controls the maximum size of YB-Master and YB-TServer log files.
Because of that, I usually prefer not to make logrotate responsible for rotating YugabyteDB logs again.
Instead, use a cleanup or archive job that works on files YugabyteDB has already rotated.
In other words:
● Use YugabyteDB for log creation and rotation.
● Use operating system tooling for retention and archival.
Be Careful With find
The find command is powerful, but it can also be dangerous if the path is too broad.
Good:
find /home/yugabyte/master/logs -type f -mtime +14 -delete
find /home/yugabyte/tserver/logs -type f -mtime +14 -delete
Risky:
find /home/yugabyte -type f -mtime +14 -delete
find /mnt/d0/yb-data -type f -mtime +14 -delete
The second examples are too broad. They may match files that are not diagnostic logs.
⚠️ Safety Tip
Always target explicit log directories. Avoid broad cleanup commands against parent YugabyteDB data directories.
That tip is useful for viewing logs and is focused on managing log retention.
Final Takeaway
YugabyteDB already rotates logs, but you may still want an explicit retention policy.
For simple cleanup, use Linux cron or a systemd timer on each DB node to delete logs older than a set number of days.
For longer retention, archive logs to a shared filesystem, object storage, or centralized logging platform before deleting them locally.
Just keep the boundary clear:
● pg_cron is for scheduled SQL.
The operating system is for scheduled file management.
And because YugabyteDB logs are node-local, OS-level retention jobs need to run on every DB node unless log collection is centralized.
✅ Final Takeaway
Use pg_cron for scheduled database work. Use operating system tools for log retention. Since YugabyteDB logs are local to each node, run your cleanup or archive job on every DB node, or use centralized logging to manage retention outside the cluster.