How to clear Action Scheduler logs

WordPress Action Scheduler is known for bloating the WordPress site database, therefore some additional tweaks must be applied for optimal performance. Further below you will see a code snippets which you can use in your WordPress theme functions.php file ar place them in a must-use plugin.

Setting a shorter data retention period

Action Scheduler data retention period along with the action_scheduler_retention_period filter defines how long the information about Completed or Cancelled actions should be kept. The default data retention period for Action Scheduler is 30 days, which in most cases is not needed and you can just set it, to let’s say – 3 days or a week.

This will set the data retention period to 3 days:

add_filter( 'action_scheduler_retention_period', 'dwp_action_scheduler_retention_period' );

function dwp_action_scheduler_retention_period() {
 return 3 * DAY_IN_SECONDS;
}

If you want to auto-delete also the Failed actions from the log, you can use the action_scheduler_default_cleaner_statuses filter.

Add this additional code snippet to include the Failed actions in the data retention policy:

add_filter( 'action_scheduler_default_cleaner_statuses', function ( $statuses ) {
	$statuses[] = 'failed';
	return $statuses;
} );

Change the batch size for each Action Scheduler run

Action Scheduler runs each time when a WP-Cron is being executed and when it runs, it processes only a limited amount of Past-due actions. This amount by default is set to 20, however there may be a need to increase it, especially if there’s often a buildup in the Past-due actions. An increased value of course will have its cost on server resources, so be careful with adjusting this.

Use this code snippet to set the batch size for Action Scheduler to 250:

add_filter( 'action_scheduler_cleanup_batch_size', function ( $batch_size ) {
	return 250;
} );

Periodic maintenance for cleaning up the old data

Add this CRON task to periodically cleanup the Action Scheduler tables from old data.

add_action('wp', function() { 
	if ( ! wp_next_scheduled( 'dwp_actionscheduler_maintenance_daily' ) ) {
		wp_schedule_event( time(), 'daily', 'dwp_actionscheduler_maintenance_daily' );
	}
});


add_action('dwp_actionscheduler_maintenance_daily', 'dwp_actionscheduler_maintenance');
function dwp_actionscheduler_maintenance() {
global $wpdb;

//Deletes Action Scheduler log entries which are older than 3 days
$wpdb->query('DELETE FROM `'.$wpdb->prefix.'actionscheduler_logs` WHERE log_date_gmt < NOW() - INTERVAL 3 DAY');

//Deletes Action Scheduler actions (completed / failed / cancelled) which are older than 3 days
$wpdb->query('DELETE FROM `'.$wpdb->prefix.'actionscheduler_actions` WHERE `status` IN ( \'canceled\', \'failed\', \'complete\' ) AND last_attempt_gmt < NOW() - INTERVAL 3 DAY');

//Deletes Action Scheduler claims which are older than 7 days
$wpdb->query('DELETE FROM `'.$wpdb->prefix.'actionscheduler_claims` WHERE date_created_gmt < NOW() - INTERVAL 7 DAY');
}

Leave a Comment on How to clear Action Scheduler logs
About the author
I'm a full-stack WordPress developer with a 10+ years of solid experience in the core web development languages, development processes / techniques, web security, Linux server management and with pretty good understanding about proper semantics, UX/UI, technical SEO, good design and basic knowledge of company leadership. On top of that - a distant 5+ years experience as a computer and electronics repair technician which often enables me to understand also how the things work at the hardware level.
Your feedback matters!…
I hope you found this article helpful. Feel free to add some comments - your feedback is very important to me, as it drives my motivation and helps me to improve the content.