We’re going to be talking about how to go about clearing completed tasks in the WordPress Action Scheduler for database maintenance.
I was working on my own websites whilst trying to debug an issue with an analytics plugin and noticed that in the Scheduled Actions list, there was quite a significant number of tasks. There was close to 14,000 tasks.
The database size at the moment is 5.52 MB with an overhead of 4 MB which can slow the website down. There are a number of ways to clean this up and one of the easiest ways for me to do this is via phpMyadmin SQL.

As you can see in the image above, there are a few tabs or statuses being recorded. There is canceled, Complete, Pending & Failed.
The large number of records, which usually gets cleared after a specific duration automatically, can make the database quite large and some cleaning could be required.
When it comes to cleaning the database, it is practicable to only clear out records that have been completed, such as failed, canceled & completed because the rest is scheduled actions or are currently running.


When in phpMyadmin SQL there are three commends we can run and before you do, for best practices, use a backup plugin such as Updraft Plus with WP Optimize to create a backup of your WordPress database.
You can also find out what database you’re using in the event you’ve got multiple mySQL databases to validate which database you’re working on.


The SQL commands
Depending on your database name prefixes, these are the SQL commands to use to clear the WordPress Scheduled Actions database.
DELETE FROM `wp_actionscheduler_actions` WHERE `status` = 'canceled'
DELETE FROM `wp_actionscheduler_actions` WHERE `status` = 'failed'
DELETE FROM `wp_actionscheduler_actions` WHERE `status` = 'complete'
That’s all you need to do to clear out the WordPress Scheduled Actions but there is also the table that has the log files. This table is called the wp_actionscheduler_logs and can get bloated but this can be easily fixed whilst within phpMyadmin.
As you can see in the image below, there’s 47,000+ records and some are dated 2020.

The only thing that needs to be done with this table is press ‘Empty‘ and that will clear out the database.

Remember, the Action Scheduler usually clears itself out after 30 days. So you don’t need to worry so much about doing this sort of task.
add_filter function
Was exploring other ways to clear out a clogged Action Scheduler and Nick Green provided an article that covers a more advanced method of clearing out a clogged action scheduler.
You can install this code into the functions.php file of the theme or what’s more recommended and safer from theme updates and losing custom functionality from not using a child theme, Code Snippets.
add_filter( 'action_scheduler_retention_period',
function() { return DAY_IN_SECONDS * 14; } );
You can read more about this method from Nick Green’s article Clearing a Clogged Action Scheduler Queue – WooCommerce
