Controlling Woo Action Scheduler

green and grey circuit board

To limit the database size for the WooCommerce Action Scheduler and its related logs table, you can implement a cleanup mechanism to remove old completed actions and logs. This can be done using the built-in functionality of WooCommerce or by writing custom code.

Option 1: Use WooCommerce’s Built-in Cleanup

WooCommerce provides a built-in setting to automatically clean up completed actions and logs after a certain period. You can configure this in your WordPress admin dashboard:

  1. Go to WooCommerce > Settings > Advanced > Action Scheduler.
  2. Set the Retention Period for completed actions and logs (e.g., 30 days, 60 days, etc.).
  3. Save the changes.

This will automatically delete completed actions and logs older than the specified retention period.


Option 2: Custom Code to Limit Database Size

If you want more control over the cleanup process, you can use custom code to limit the size of the wp_actionscheduler_actions and wp_actionscheduler_logs tables.

Add the following code to your theme’s functions.php file or a custom plugin:

function limit_action_scheduler_db_size() {
    global $wpdb;

    // Define the maximum number of rows to keep in the actions table
    $max_actions = 10000; // Adjust this number as needed

    // Define the maximum number of rows to keep in the logs table
    $max_logs = 10000; // Adjust this number as needed

    // Clean up old actions
    $actions_table = $wpdb->prefix . 'actionscheduler_actions';
    $logs_table = $wpdb->prefix . 'actionscheduler_logs';

    // Delete old actions
    $wpdb->query(
        $wpdb->prepare(
            "DELETE FROM {$actions_table} WHERE action_id <= (
                SELECT action_id FROM (
                    SELECT action_id FROM {$actions_table} 
                    ORDER BY action_id DESC 
                    LIMIT 1 OFFSET %d
                ) AS subquery
            )",
            $max_actions
        )
    );

    // Delete old logs
    $wpdb->query(
        $wpdb->prepare(
            "DELETE FROM {$logs_table} WHERE log_id <= (
                SELECT log_id FROM (
                    SELECT log_id FROM {$logs_table} 
                    ORDER BY log_id DESC 
                    LIMIT 1 OFFSET %d
                ) AS subquery
            )",
            $max_logs
        )
    );
}

// Schedule the cleanup to run daily
if (!wp_next_scheduled('limit_action_scheduler_db_size')) {
    wp_schedule_event(time(), 'daily', 'limit_action_scheduler_db_size');
}
add_action('limit_action_scheduler_db_size', 'limit_action_scheduler_db_size');

This code will:

  1. Define a maximum number of rows to keep in the wp_actionscheduler_actions and wp_actionscheduler_logs tables.
  2. Delete older rows that exceed the defined limits.
  3. Schedule the cleanup to run daily.

Option 3: Manually Clean Up via SQL

If you prefer to manually clean up the tables, you can run SQL queries directly in your database management tool (e.g., phpMyAdmin):

-- Delete completed actions older than 30 days
DELETE FROM wp_actionscheduler_actions 
WHERE status = 'complete' 
AND scheduled_date_gmt < DATE_SUB(NOW(), INTERVAL 30 DAY);

-- Delete logs older than 30 days
DELETE FROM wp_actionscheduler_logs 
WHERE log_date_gmt < DATE_SUB(NOW(), INTERVAL 30 DAY);

Notes:

  • Always back up your database before performing manual deletions or running custom code.
  • Adjust the retention period or row limits based on your site’s needs and server resources.
  • Monitor the database size and performance after implementing these changes.

By implementing one of these solutions, you can effectively limit the size of the WooCommerce Action Scheduler tables and prevent them from growing excessively large.

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.