Database Cleanup Utility

A guide to performing database cleanup.

On this page:

Introduction

This document provides comprehensive instructions for executing database cleanup operations across MySQL, Oracle, and Microsoft SQL Server environments. The cleanup utility removes stale data from OAuth tokens, audit logs, board items, alerts, usage statistics, SLA records, and system tables based on the configurable retention periods.

Prerequisites

Before you begin, ensure you meet the following prerequisites:

General requirements

Component Requirement
Operating System

Linux (RHEL/CentOS/Ubuntu) or Windows Server 2016+

Note: For Windows environments, the utility is available as PowerShell scripts.

Database Access Valid credentials with DELETE privileges.
Network Connectivity to the database server on the required port

Database-specific tools

Database Required Tool Installation Path
MySQL mysql client

/usr/local/mysql/bin/mysql or $PATH

Example: export PATH="/usr/local/mysql/bin:$PATH"

Oracle sqlplus

/opt/oracle/instantclient or $PATH

Example: export PATH="/opt/oracle/instantclient_19_8:$PATH"

MSSQL sqlcmd

Included with SQL tools

Example: C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\sqlcmd.exe

Supported database versions

Database Supported Versions Tested On
MySQL 5.7, 8.0+ 8.0.32
Oracle 19c, 21c 21c
MSSQL 2016–2022 2019

Cleanup categories and SQL file descriptions

Provides an overview of cleanup categories and associated SQL file descriptions for removing obsolete and expired data.

OAuth cleanup (cleanup_oauth_*.sql)

Purpose: Removes the expired OAuth 2.0 grant data, access tokens, refresh tokens, and related metadata.

Tables affected:

Table Name Description
OAUTH_GRANTS Stores authorization grants with statuses such as Rejected, Error, Expired, ClientDeleted, Revoked, and Cancelled.
OAUTH_TOKENS Stores the access tokens and refresh tokens.
OAUTH_GRANT_PROPERTIES Stores the metadata associated with grants.
OAUTH_MAC_TOKENS Stores the MAC token data.
OAUTH_JWT_TOKENS Stores the JWT-based tokens.
OAUTH_RO_AUTH_INSTRUCTIONS Stores the resource owner authorization instructions.
OAUTH_GRANT_RO_USERINFO / OAUTH_RO_USERINFO_EXTN Stores the resource owner user information.
OPENID_CONNECT_GRANTS Stores the OpenID Connect grant data.

Retention period: 365 hours (subject to the token expiry configuration).

Audit cleanup (cleanup_audit_*.sql)

Purpose: Purges the old audit trail records to maintain compliance and improve performance.

Tables affected:

Table Name Description
PM_AUDITTRAIL Stores the Policy Manager audit event records.
AM_AUDITTRAIL Stores the Analytics audit information records.

Retention period: 168 hours (7 days) in accordance with compliance requirements.

Board Items and Notifications (cleanup_delete_board_items_dn_wf_notifications_*.sql)

Purpose: Removes the stale discussion items, alerts, notifications, and workflow-related data from the Community Manager portal dashboard.

Tables affected:

Table Name Description
BOARD_ITEMS Stores the board item content.
BOARD_ITEM_ASSIGNMENTS Stores the user assignments associated with board items.
DISTINGUISHED_NAMES Stores the object naming and identification for discussions, alerts, and notification types.
WORKFLOW_DN Stores the workflow distinguished name associations.
NOTIFICATIONS Stores the system notification queue entries (orphaned cleanup).

Retention period:

High-volume systems: 1–24 hours.

Standard deployments: 168 hours (7 days) or as defined by organization requirements.

Alerts cleanup (cleanup_alerts_*.sql)

Purpose: Provides a dedicated cleanup of the system alert tables.

Tables affected:

Table Name Description
AM_EMAILALERTS Stores the fired email alert records generated by the system.
AM_ALERTS_SLAS Stores the fired SLA-based alert records.
AM_ALERTCOMMENTS Stores the comments associated with fired alerts.
AM_ALERTS Stores the fired alert records.

Retention period: 168 hours or as defined by organization requirements.

Usage Statistics (cleanup_usage_*.sql)

Purpose: Removes the aggregated usage statistics and metrics data.

Tables affected:

Table Name Description
MO_USAGE_NEXTHOP Stores the references to downstream (next‑hop) usage data records.
MO_USAGEMSGS Stores the service and API call (request, response, and fault) message content.
MO_USAGEDATA Stores the metadata for Service and API calls managed by the API Gateway, controlled by the Auditing Policy.

Retention period: 720 hours (3–30 days) for trend analysis or as defined by organization requirements.

SLA cleanup (cleanup_sla_*.sql)

Purpose: Purges the outdated Service Level Agreement (SLA) monitoring data and breach records.

Tables affected:

Table Name Description
API_CONTRACT_SLA_DATA Stores the SLA monitoring and metrics data at the contract level.
API_SLA_DATA Stores the SLA monitoring and breach data for APIs.

Retention period: 168–720 hours (7–30 days) or as defined by organization requirements.

OS (Operational Store) cleanup (cleanup_os_*.sql)

Purpose: Cleans the Operational Store (OS) tables by removing outdated data to improve overall system performance.

Tables affected:

Table Name Description
OS_HISTORYSTEP_PREV Stores the archived historical step data (previous state).
OS_CURRENTSTEP_PREV Stores the archived current step data (previous state).
OS_HISTORYSTEP Stores the historical step records.
OS_CURRENTSTEP Stores the current step records.
OS_WFENTRY Stores the WF (entry) records.

Retention period: 24–168 hours (1–7 days).

Rollup/Aggregation cleanup (cleanup_rollup_*.sql)

Purpose: Removes the rolled‑up and aggregated data that has been archived or superseded.

Tables affected:

Table Name Description
MO_ROLLUPDATA Stores 5-second rollup data transmitted by each Network Director.
MO_ROLLUP15 Stores 15-minute rollups derived from the Policy Manager Scheduled Jobs.
MO_ROLL_ORG15 Stores 15-minute rollups per business (organization) derived by the Policy Manager Scheduled Jobs.
MO_ROLLUP_HOUR Stores hourly rollups derived by the Policy Manager Scheduled Jobs.
MO_ROLL_ORG_H Stores hourly rollups per business (organization) derived by the Policy Manager Scheduled Jobs.
MO_ROLLUP_DAY Stores daily rollups derived by the Policy Manager Scheduled Jobs.
MO_ROLL_ORG_D Stores daily rollups per business (organization) derived by the Policy Manager Scheduled Jobs.

Retention period: 720+ hours (30+ days) or as defined by organization requirements.

Configuration parameters

This section describes the global parameters, per‑file configuration format, and database connection parameters required to configure the utility.

Global parameters (All databases)

Parameter Description Default
MAX_LOOPS Maximum iterations before forced exit 1
BATCH_SLEEP Pause duration between iterations 5 seconds
BASE_DIR Working directory used for scripts and logs Current directory
LOGFILE Path for statistics output logs db_cleanup_*_stats.log
SQL_LOGFILE Path for executed SQL statement logs db_cleanup_*_sql.log

Per-file configuration format

Position Field Description Example
1 File Path Absolute or relative path to the SQL file /opt/scripts/cleanup_oauth.sql
2 Hours Default data retention period (in hours) 24
3 Batch Size Default number of rows to delete per batch 1000

Configuration examples (bash):

Copy
# Full configuration (path + hours + batch)
/opt/cleanup_oauth.sql|24|1000
# Hours only (uses default batch)
/opt/cleanup_audit.sql|48
# Path only (uses both defaults)
/opt/cleanup_alerts.sql

Database connection parameters

MySQL

Parameter Description Example
DB_HOST MySQL server hostname or IP 0.0.0.0 or mysql.example.com
DB_PORT MySQL port 3306
DB_USER Database username root
DB_PASS Database password <db_password>
DB_NAME Target database name eap2026_1_0

Oracle

Parameter Description Example
DB_HOST Oracle server hostname akana-qa-oracle21c.ck7brgzh0xju.us-east-1.rds.amazonaws.com
DB_PORT Oracle listener port 1521
DB_NAME Oracle SID or service name ORCL
DB_USER Schema username eap2026_1_0_user
DB_PASS Schema password <db_password>

MSSQL

Parameter Description Example
SERVER SQL Server instance tcp:server.ck7brgzh0xju.us-east-1.rds.amazonaws.com,1433
DATABASE Database name eap2026_1_qa112
DB_USER SQL Server login eap2026_1_qa112_user
DB_PASS Login password <db_password>

Execution instructions

Before you begin, ensure that all prerequisites are met and that the cleanup script is available.

In SQL file entries that use pipe (|)‑delimited values, the first value specifies the retention period (in hours) and the second value specifies the batch size (the number of rows deleted per batch). If no pipe (|) delimiter is specified, the default values apply: batch size of 1,000 rows per batch and retention period of 24 hours.

MySQL

Use the MySQL cleanup script to remove outdated data from selected database tables based on configured retention periods and batch sizes. To view sample database cleanup scripts, download the zip archive: db_cleanup_scripts.zip.

Step 1: Setup the environment

  1. Verify the active MySQL installation by running the following command:
    Copy
    which mysql
    # Expected output: /usr/local/mysql/bin/mysql
  2. Enable execution of the cleanup script by running the following command:
    Copy
    chmod +x cleanup_mysql.sh

Step 2: Configure the script

  1. Specify the required database connection details.
    Copy
    # Database Credentials (REQUIRED)
    DB_USER="your_username"
    DB_PASS="your_password"
    DB_NAME="your_db"
    DB_HOST="mysql.host"
    DB_PORT=3306
  2. Specify the SQL files to execute by commenting or uncommenting the files that need to be executed. The following SQL files are executed sequentially.
    Copy
    declare -a SQLFILES_WITH_CONFIG=(
    "$BASE_DIR/cleanup_oauth_mysql.sql|1|8"
    "$BASE_DIR/cleanup_audit_mysql.sql|2|7"
    "$BASE_DIR/cleanup_delete_board_items_dn_wf_notifications_mysql.sql|3|6"
    "$BASE_DIR/cleanup_alerts_mysql.sql|4|5"
    "$BASE_DIR/cleanup_usage_mysql.sql|5|4"
    "$BASE_DIR/cleanup_sla_mysql.sql|6|3"
    "$BASE_DIR/cleanup_os_mysql.sql|7|2"
    "$BASE_DIR/cleanup_rollup_mysql.sql|8|1"
    )

Step 3: Execute the cleanup script

Run the script by using the following command:

Copy
./cleanup_mysql.sh

Oracle

Use the Oracle cleanup script to remove outdated data from selected database tables based on configured retention periods and batch sizes. To view sample database cleanup scripts, download the zip archive: db_cleanup_scripts.zip.

Step 1: Setup the environment

  1. Verify that the Oracle client tools are installed and accessible by running the following command:
    Copy
    sqlplus -v
  2. Enable execution of the cleanup script by running the following command:
    Copy
    chmod +x cleanup_oracle.sh

Step 2: Configure the script

  1. Specify the required database connection details.
    Copy
    DB_USER="schema_user"
    DB_PASS="password"
    DB_NAME="ORCL"
    DB_HOST="oracle.host"
  2. Specify the SQL files to execute by commenting or uncommenting the files that need to be executed. The following SQL files are executed sequentially.
    Copy
    declare -a SQLFILES_WITH_CONFIG=(
        "$BASE_DIR/cleanup_oauth_oracle.sql"
        "$BASE_DIR/cleanup_audit_oracle.sql|2|7"
        "$BASE_DIR/cleanup_delete_board_items_dn_wf_notifications_oracle.sql|3|6"
        "$BASE_DIR/cleanup_alerts_oracle.sql|4|5"
        "$BASE_DIR/cleanup_usage_oracle.sql|5|4"
        "$BASE_DIR/cleanup_sla_oracle.sql|6|3"
        "$BASE_DIR/cleanup_os_oracle.sql|7|2"
        "$BASE_DIR/cleanup_rollup_oracle.sql|8|1"
    )

Step 3: Execute the cleanup script

Run the script by using the following command:

Copy
./cleanup_oracle.sh

MSSQL (Linux)

Use the MSSQL cleanup script to remove outdated data from selected database tables based on configured retention periods and batch sizes. To view sample database cleanup scripts, download the zip archive: db_cleanup_scripts.zip.

Step 1: Setup the environment

Install the required Microsoft SQL Server client tools by running the following CURL command:

Copy
# Install tools
curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo
ACCEPT_EULA=Y yum install -y msodbcsql17 mssql-tools

Step 2: Configure the script

  1. Specify the required database connection details.
    Copy
    SERVER="tcp:your-server,1433"
    DATABASE="your_db"
    DB_USER="user"
    DB_PASS="pass"
  2. Specify the SQL files to execute by commenting or uncommenting the files that need to be executed. The following SQL files are executed sequentially.
    Copy
    declare -a SQLFILES_WITH_CONFIG=(
        "$BASE_DIR/cleanup_oauth_mssql.sql"
        "$BASE_DIR/cleanup_audit_mssql.sql|2|7"
        "$BASE_DIR/cleanup_delete_board_items_dn_wf_notifications_mssql.sql|3|6"
        "$BASE_DIR/cleanup_alerts_mssql.sql|4|5"
        "$BASE_DIR/cleanup_usage_mssql.sql|5|4"
        "$BASE_DIR/cleanup_sla_mssql.sql|6|3"
        "$BASE_DIR/cleanup_os_mssql.sql|7|2"
        "$BASE_DIR/cleanup_rollup_mssql.sql|8|1"
    )

Step 3: Execute the cleanup script

Run the script by using the following command:

Copy
./cleanup_mssql.sh

MSSQL (Windows)

Use the MSSQL cleanup script to remove outdated data from selected database tables based on configured retention periods and batch sizes. To view sample database cleanup scripts, download the zip archive: db_cleanup_scripts.zip.

Step 1: Setup the environment

Verify that the SQL Server command-line tools are installed by running the following command:

Copy
sqlcmd -?

Step 2: Configure the script

  1. Update the required database connection details.
    Copy
    set "SERVER=tcp:server,1433"
    set "DATABASE=db"
    set "DB_USER=user"
    set "DB_PASS=pass"
  2. Specify the SQL files to execute by commenting or uncommenting the files that need to be executed. The following SQL files are executed sequentially.
    Copy
    set "SQLCONFIG0=%BASE_DIR%\cleanup_oauth_mssql.sql|1|8"
    set "SQLCONFIG1=%BASE_DIR%\cleanup_audit_mssql.sql|2|7"
    set "SQLCONFIG2=%BASE_DIR%\cleanup_delete_board_items_dn_wf_notifications_mssql.sql|3|6"
    set "SQLCONFIG3=%BASE_DIR%\cleanup_alerts_mssql.sql|4|5"
    set "SQLCONFIG4=%BASE_DIR%\cleanup_usage_mssql.sql|5|4"
    set "SQLCONFIG5=%BASE_DIR%\cleanup_sla_mssql.sql|6|3"
    set "SQLCONFIG6=%BASE_DIR%\cleanup_os_mssql.sql|7|2"
    set "SQLCONFIG7=%BASE_DIR%\cleanup_rollup_mssql.sql"

Step 3: Execute the cleanup script

Run the script by using the following command:

Copy
cleanup_mssql.bat

Database cleanup execution log format

Log files summarize database cleanup activity. Statistics logs (db_cleanup_*_stats.log) show execution details and deletion counts, while SQL logs (db_cleanup_*_sql.log) record the executed statements.

The following is a sample execution log format:

  • db_cleanup_*_stats.log: Contains execution statistics, including row counts and timing information.
Copy
Cleanup started at Wed Mar  4 09:38:24 MST 2026
Total no. of iterations: 2
Default retention hours: 24
Default rows per batch: 1000
 Per-file configuration:
  File                                                         |  Hours | Batch
  -----------------------------------------------------------------------------

  cleanup_oauth_mysql.sql                                      |     1h |   8       

  cleanup_audit_mysql.sql                                      |     2h |   7      

  cleanup_delete_board_items_dn_wf_notifications_mysql.sql     |     3h |   6       

  cleanup_alerts_mysql.sql                                     |     4h |   5      

  cleanup_usage_mysql.sql                                      |     5h |   4       

  cleanup_sla_mysql.sql                                        |     6h |   3      

  cleanup_os_mysql.sql                                         |     7h |   2       

  cleanup_rollup_mysql.sql                                     |     8h |   1  

============================================================
== ITERATION 1 :: Wed Mar  4 09:38:25 MST 2026
============================================================
--- Running SQL file: cleanup_oauth_mysql.sql (retention: 1h, batch: 8) at Wed Mar  4 09:38:25 MST 2026 ---
Results for cleanup_oauth_mysql.sql:

 OAUTH_GRANT_PROPERTIES                   rows=0        time=2ms

 OAUTH_RO_AUTH_INSTRUCTIONS               rows=0        time=2ms

 OAUTH_GRANT_RO_USERINFO                  rows=0        time=5ms

 OAUTH_TOKENS                             rows=0        time=2ms

 OAUTH_MAC_TOKENS                         rows=0        time=1ms

 OPENID_CONNECT_GRANTS                    rows=0        time=1ms

 OAUTH_RO_USERINFO_EXTN                   rows=0        time=1ms

 OAUTH_JWT_TOKENS                         rows=0        time=1ms

 OAUTH_GRANTS                             rows=0        time=5ms

--- Completed: cleanup_oauth_mysql.sql ---
--- Running SQL file: cleanup_audit_mysql.sql (retention: 2h, batch: 7) at Wed Mar  4 09:38:25 MST 2026 ---
Results for cleanup_audit_mysql.sql:

 PM_AUDITTRAIL                            rows=0        time=2ms

 AM_AUDITTRAIL                            rows=0        time=1ms

--- Completed: cleanup_audit_mysql.sql ---
--- Running SQL file: cleanup_audit_mysql.sql (retention: 2h, batch: 7) at Wed Mar  4 09:38:31 MST 2026 ---
Results for cleanup_audit_mysql.sql:

 PM_AUDITTRAIL                            rows=0        time=2ms

 AM_AUDITTRAIL                            rows=0        time=1ms

--- Completed: cleanup_audit_mysql.sql ---
…………
…………
…………
**************** ITERATION SUMMARY (Iteration 2) ****************
tables_with_deletes=14 total_rows=59 iteration_time=1s
********************************************************************
Max loop limit reached. Stopping cleanup for safety.
Cleanup completed at Wed Mar  4 09:38:37 MST 2026
=========================================
  • db_cleanup_*_sql.log: Contains the actual SQL statements executed during the cleanup.
Copy
=== New run started at 2026-03-04T09:38:24-07:00 ===

DELETE gp FROM OAUTH_GRANT_PROPERTIES gp JOIN (   SELECT g.GRANTSEQID   FROM OAUTH_GRANTS g   WHERE g.STATUS IN ('Rejected','Error','Expired','ClientDeleted','Revoked','Cancelled')     AND g.UPDATED < TIMESTAMPADD(HOUR, -1, NOW())   ORDER BY g.UPDATED   LIMIT 8) s ON s.GRANTSEQID = gp.GRANTSEQID;

DELETE rai FROM OAUTH_RO_AUTH_INSTRUCTIONS rai JOIN (   SELECT g.GRANTSEQID   FROM OAUTH_GRANTS g   WHERE g.STATUS IN ('Rejected','Error','Expired','ClientDeleted','Revoked','Cancelled')     AND g.UPDATED < TIMESTAMPADD(HOUR, -1, NOW())   ORDER BY g.UPDATED   LIMIT 8) s ON s.GRANTSEQID = rai.GRANTSEQID;

DELETE grui FROM OAUTH_GRANT_RO_USERINFO grui JOIN (   SELECT g.GRANTSEQID   FROM OAUTH_GRANTS g   WHERE g.STATUS IN ('Rejected','Error','Expired','ClientDeleted','Revoked','Cancelled')     AND g.UPDATED < TIMESTAMPADD(HOUR, -1, NOW())   ORDER BY g.UPDATED   LIMIT 8) s ON s.GRANTSEQID = grui.GRANTSEQID;

DELETE ot FROM OAUTH_TOKENS ot JOIN (   SELECT g.GRANTSEQID   FROM OAUTH_GRANTS g   WHERE g.STATUS IN ('Rejected','Error','Expired','ClientDeleted','Revoked','Cancelled')     AND g.UPDATED < TIMESTAMPADD(HOUR, -1, NOW())   ORDER BY g.UPDATED   LIMIT 8) s ON s.GRANTSEQID = ot.GRANTSEQID;

DELETE omt FROM OAUTH_MAC_TOKENS omt JOIN (   SELECT g.GRANTSEQID   FROM OAUTH_GRANTS g   WHERE g.STATUS IN ('Rejected','Error','Expired','ClientDeleted','Revoked','Cancelled')     AND g.UPDATED < TIMESTAMPADD(HOUR, -1, NOW())   ORDER BY g.UPDATED   LIMIT 8) s ON s.GRANTSEQID = omt.GRANTSEQID;

DELETE ocg FROM OPENID_CONNECT_GRANTS ocg JOIN (   SELECT g.GRANTSEQID   FROM OAUTH_GRANTS g   WHERE g.STATUS IN ('Rejected','Error','Expired','ClientDeleted','Revoked','Cancelled')     AND g.UPDATED < TIMESTAMPADD(HOUR, -1, NOW())   ORDER BY g.UPDATED   LIMIT 8) s ON s.GRANTSEQID = ocg.GRANTSEQID;

DELETE ruie FROM OAUTH_RO_USERINFO_EXTN ruie JOIN (   SELECT g.GRANTSEQID   FROM OAUTH_GRANTS g   WHERE g.STATUS IN ('Rejected','Error','Expired','ClientDeleted','Revoked','Cancelled')     AND g.UPDATED < TIMESTAMPADD(HOUR, -1, NOW())   ORDER BY g.UPDATED   LIMIT 8) s ON s.GRANTSEQID = ruie.GRANTSEQID;

DELETE jwt FROM OAUTH_JWT_TOKENS jwt JOIN (   SELECT g.GRANTSEQID   FROM OAUTH_GRANTS g   WHERE g.STATUS IN ('Rejected','Error','Expired','ClientDeleted','Revoked','Cancelled')     AND g.UPDATED < TIMESTAMPADD(HOUR, -1, NOW())   ORDER BY g.UPDATED   LIMIT 8) s ON s.GRANTSEQID = jwt.GRANTSEQID;

DELETE g FROM OAUTH_GRANTS g JOIN (   SELECT GRANTSEQID   FROM OAUTH_GRANTS   WHERE STATUS IN ('Rejected','Error','Expired','ClientDeleted','Revoked','Cancelled')     AND UPDATED < TIMESTAMPADD(HOUR, -1, NOW())   ORDER BY UPDATED   LIMIT 8) s ON s.GRANTSEQID = g.GRANTSEQID;

DELETE FROM PM_AUDITTRAIL WHERE DTSCREATE < TIMESTAMPADD(HOUR, -2, NOW()) LIMIT 7;

DELETE FROM AM_AUDITTRAIL WHERE CREATEDTS < TIMESTAMPADD(HOUR, -2, NOW()) LIMIT 7;
…………
…………
…………
…………

Troubleshooting

Use this section to identify common errors, determine the underlying cause, and resolve the issue.

Common issues and solutions

Error message Cause Resolution
mysql/sqlplus/sqlcmd not found Client binary is not in the system PATH. Install the required client tools or update the PATH environment variable.
File not readable Incorrect file path or insufficient permissions. Verify file permissions and use absolute paths.
Invalid config format Too many/few pipe separators Ensure format is path\nhours\nbatch (no extra pipes in path).
Connection refused Network connectivity or firewall issue. Verify the host, port, and security group settings.
Login failed Invalid credentials. Confirm the username, password, and database permissions.
No rows deleted Data is newer than the retention period. Verify HOURS_TO_KEEP and confirm data timestamps.
Script exits after 1 iteration NON_ZERO_TABLES=0 or MAX_LOOPS=1. Check the log for "All tables cleaned" or increase MAX_LOOPS.

Log analysis

After running the database cleanup utility, review the generated log files to identify errors, warnings, or unexpected behavior. Log analysis helps you quickly verify whether the operation completed successfully or requires follow‑up investigation.

Use standard command‑line tools to scan the log files for error messages. Most cleanup utilities write status information to plain‑text logs, making them easy to search.

The following commands search a database cleanup log file for lines that contain the word error. They help you quickly identify whether the cleanup utility encountered problems during execution. Although the examples are grouped by database type, the behavior is identical, only the log file name changes.

Copy
# MySQL/Linux
grep -i "error" db_cleanup_mysql_stats.log
# Oracle/Linux
grep -i "error" db_cleanup_oracle_stats.log
# MSSQL/Linux
grep -i "error" db_cleanup_mssql_stats.log
# Windows (PowerShell)
Select-String -Path "db_cleanup_mssql_stats.log" -Pattern "error"

Performance tuning

Performance tuning helps you balance cleanup speed, database load, and system stability. The cleanup utility processes data in batches. By adjusting batch size and pause intervals, you can tailor execution to match your workload, database size, and operational constraints.

The following parameters control cleanup behavior:

  • ROWS_PER_BATCH: Number of rows processed in a single transaction.
  • BATCH_SLEEP: Time (in seconds) the utility waits before processing the next batch.

The following table provides guidance for adjusting batch size and sleep intervals based on your environment:

Scenario Consideration
Large data volumes Increase ROWS_PER_BATCH (for example, 5,000–10,000).
High concurrency environment Decrease ROWS_PER_BATCH (for example, 100–500) and increase BATCH_SLEEP.
Urgent cleanup needed Reduce BATCH_SLEEP to 0–1 seconds.
Off-peak processing Schedule execution using cron (Linux) or Task Scheduler (Windows).

Cron scheduling: Example (Linux)

Use cron to run database cleanup scripts automatically at scheduled times. The following examples show how to configure daily and weekly cleanup jobs and capture output in a log file.

Copy
# Edit crontab
crontab -e
# Run daily at 2 AM
0 2 * * * /opt/scripts/cleanup_mysql.sh >> /var/log/cleanup_cron.log 2>&1
# Run weekly (Sundays at 3 AM)
0 3 * * 0 /opt/scripts/cleanup_oracle.sh >> /var/log/cleanup_cron.log 2>&1

Windows task scheduler

Use Windows Task Scheduler to run the database cleanup script automatically at a defined time. Follow these steps:

  1. Open Task Scheduler.
  2. Select Create Basic Task.
  3. Specify a name for the task. For example, database cleanup.
  4. Set the trigger to Daily at 2:00 AM.
  5. Set the action to Start a program.
  6. Configure:
    • Program: C:\scripts\cleanup_mssql.bat
    • Arguments (optional): --loops 3 --sleep 10