Chuck Grimmett

Action Scheduler clean up

Action Scheduler is a library for triggering a WordPress hook to run at some time in the future. It is used in a lot of large plugins to handle background processing of large job queues. It is an extremely useful tool. Unfortunately, it is also easy for bugs to make the queue or logs explode in size.

I had a fun runaway action scheduler issue to deal with at work today that ended up creating 26.7M rows in the wp_actionscheduler_actions table before we patched the bug, so I had to figure out how to clean it up. I’m blogging about what I learned so future me can doesn’t have to figure it out again.

Action Scheduler Defaults

By default, after every run, Action Scheduler cleans up completed and canceled actions older than 31 days. It does not clean up failed actions by default. Failed actions will stay in the wp_actionscheduler_actions table until manually cleaned up.

You can change the default retention period or statuses that are cleaned up with filters:

action_scheduler_default_cleaner_statuses

action_scheduler_retention_period

Action Scheduler CLI

wp action-scheduler clean

wp action-scheduler clean --batch-size=500 --batches=1000

wp action-scheduler clean --status=failed --batch-size=50 --before='90 days ago'

Bash

Bash and sql to delete 50K rows at a time with 2s delay after each delete, pausing to re-assess every 1M rows:

for i in $(seq 1 20); do echo "Batch $i/20..."; wp db query "DELETE FROM wp_actionscheduler_actions WHERE status = 'canceled' LIMIT 50000;"; sleep 2; done

Truncate

The fastest option.

  1. Export pending actions
  2. TRUNCATE TABLE wp_actionscheduler_actions;
  3. Import pending actions

Processing backed up queues

  1. First try https://github.com/woocommerce/action-scheduler-high-volume
  2. When all else fails, use the CLI, increase the batch size, --force, and if it is big enough to have multiple runs going, make it more efficient by having each run focus on a different group or hook.

Category:


Comments and Webmentions

One response to “Action Scheduler clean up”

  • Chuck Grimmett
Fediverse reactions

Leave a Reply

Webmentions

If you've written a response on your own site, you can enter that post's URL to reply with a Webmention.

The only requirement for your mention to be recognized is a link to this post in your post's content. You can update or delete your post and then re-submit the URL in the form to update or remove your response from this page.

Learn more about Webmentions.