How to Clear specified completed scheduled actions in woocommerce?

Photo of author
Written By geekerhub

Lorem ipsum dolor sit amet consectetur pulvinar ligula augue quis venenatis. 

Action Scheduler is a scalable, traceable job queue for background processing large sets of actions in WordPress. It’s specially designed to be distributed in WordPress plugins.

There are Lots of unwanted entries in the action scheduler table which is not used. So it is good to delete action scheduler entries with the status of complete action

To delete action scheduler entries we need to use action_scheduler_run_queue hook. this hook runs a queue, and maybe dispatch another async request to run another queue if there are still pending actions after completing a queue in this request.

Copy the below code and add it to your functions.php file. It will clear out all the specified completed scheduled actions, 5000 at a time.

function clear_woocommerce_scheduled_actions_geekerhub() {
    global $wpdb;

    $limit         = 5000;
    $actions_table = $wpdb->prefix . 'actionscheduler_actions';
    $logs_table    = $wpdb->prefix . 'actionscheduler_logs';
    $actions_query = sprintf(
        SELECT  action_id
        FROM    %s
        WHERE   status = 'complete'
        LIMIT   %s
    $action_ids      = $wpdb->get_col( $actions_query );
    $ids             = implode( ',', array_map( 'absint', $action_ids ) );

    if ( ! empty( $ids ) ) {
        $deleted_logs    = $wpdb->query( "DELETE FROM $logs_table WHERE action_id IN ($ids)" );
        $deleted_actions = $wpdb->query( "DELETE FROM $actions_table WHERE action_id IN ($ids)" );

add_action( 'action_scheduler_run_queue', 'clear_woocommerce_scheduled_actions_geekerhub', 999 );

Leave a Comment