How to clean up WooCommerce Action Scheduler with PHP

Action Scheduler can be accessed via “WooCommerce > Status > Scheduled Actions”.

Manual cleanup

Delete canceled, failed, and completed actions

/**
 * Deletes canceled, failed, and completed actions.
 *
 * @see https://wpcodebook.com/woocommerce-action-scheduler-cleanup-php/
 */
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->prefix}actionscheduler_actions WHERE `status` IN ( 'canceled', 'failed', 'complete' )" );

Empty Action Scheduler logs

/**
 * Empties Action Scheduler logs.
 *
 * @see https://wpcodebook.com/woocommerce-action-scheduler-cleanup-php/
 */
global $wpdb;
$wpdb->query( "TRUNCATE `{$wpdb->prefix}actionscheduler_logs`" );

Automatic cleanup

Reduce retention period

/**
 * Sets Action Scheduler retention period to 7 days.
 *
 * Default: one month.
 *
 * @see https://wpcodebook.com/woocommerce-action-scheduler-cleanup-php/
 */
add_filter( 'action_scheduler_retention_period', function ( $period ) {
	return 7 * DAY_IN_SECONDS;
} );

Include failed actions

/**
 * Sets Action Scheduler cleanup action statuses.
 *
 * Default: "complete" and "canceled"
 *
 * @see https://wpcodebook.com/woocommerce-action-scheduler-cleanup-php/
 */
add_filter( 'action_scheduler_default_cleaner_statuses', function ( $statuses ) {
	$statuses[] = 'failed';
	return $statuses;
} );

Increase batch size

/**
 * Sets Action Scheduler cleanup batch size.
 *
 * Default: 20
 *
 * @see https://wpcodebook.com/woocommerce-action-scheduler-cleanup-php/
 */
add_filter( 'action_scheduler_cleanup_batch_size', function ( $batch_size ) {
	return 100;
} );

Leave a Comment