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.
Will clear out all completed scheduled actions. Can be modified to clear out other statuses, as well.
<?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'
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