Skip to content

Instantly share code, notes, and snippets.

@jessepearson
Last active November 20, 2023 08:01
Show Gist options
  • Save jessepearson/5ad64cceb2dff2ef72b72b98d1cdfd32 to your computer and use it in GitHub Desktop.
Save jessepearson/5ad64cceb2dff2ef72b72b98d1cdfd32 to your computer and use it in GitHub Desktop.

Revisions

  1. jessepearson revised this gist May 17, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php // Do not copy this line.

    /**
    * Will clear out all the specified completed scheduled actions, 5000 at a time.
    */
  2. jessepearson renamed this gist May 17, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. jessepearson created this gist May 17, 2021.
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    /**
    * Will clear out all the specified completed scheduled actions, 5000 at a time.
    */
    function clear_woocommerce_scheduled_actions_20200609() {
    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
    ",
    $actions_table,
    $limit
    );
    $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_20200609', 999 );