Skip to content

Instantly share code, notes, and snippets.

@seb86
Forked from jessepearson/functions.php
Created January 27, 2021 12:26
Show Gist options
  • Save seb86/aa8800667de9f48a75c477526c22277b to your computer and use it in GitHub Desktop.
Save seb86/aa8800667de9f48a75c477526c22277b to your computer and use it in GitHub Desktop.
Will clear out all the specified completed scheduled actions, 5000 at a time.
<?php // do not copy this line
/**
* 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'
AND hook = 'wc-admin_import_orders'
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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment