Wordpress - Delete thousands of cron jobs

Thanks Privateer for the prompt reply and advice.

I found a way around it before I saw your answer. Here is a step-by-step method for deleting thousands of old cron jobs and may be of use to someone else.

I logged on to phpMyAdmin. I clicked on my database and then the 'search' tab. I typed in 'cron' then selected 'all tables' and clicked 'Go'. I scrolled down the search results list to my wp_options table. I clicked 'Browse'. At the top of the list was option_name 'cron'. I clicked 'Edit' then waited for the page to load. I clicked on the box that showed the list of cron jobs. The cron list was so long that it took about 80 seconds for my cursor to respond. I then used Ctrl-A on the keyboard to select all before hitting the delete button. It took about 2 minutes before my browser completed the deletion (chrome timed-out so I tried Firefox which worked).

After another couple of minutes the cron jobs for my current active plugins re-populated the list. There were 9 cron jobs (down from over 29,000!). Six years of duplicate cron jobs from badly coded plugins, some of which I just installed for a day to try out. Also hundreds from common plugins such as Wordfence, BackupBuddy, Nextgen Gallery, and AutoOptimizer - all of which I had uninstalled in the past. My site now loads like it's been turbo-charged. The admin area is much quicker. Admin timeout errors have disappeared. I had spent so much time on optimising my website trying to decrease the load time. I even moved hosts and upgraded my hosting plans. Nothing increased the speed of my site like deleting all the outdated cron jobs. Mobile download time decreased from 20 seconds to 6 seconds. Desktop download time decreased from about 12 to 4 seconds.

In my search for a solution I found very little information on the effect of cron jobs on website performance. Many said it made little difference and for a small number of cron jobs that's true. But years into the life of a WordPress site I wonder how many are bloated with hundreds if not thousands of old cron jobs from deleted plugins. Instead of asking users to check their php memory limit I would suggest that developers first ask users to check the number of cron jobs in wp_options when problem-solving fatal memory errors. You may be surprised/shocked at what you find! :-)


Try

SELECT * FROM `wp_options` WHERE option_name = 'cron'

If you find it you might try:

  • In SQL: UPDATE wp_options SET option_value = '' WHERE option_name = 'cron'
  • In wordpress: update_option('cron', '');

You might need to either delete the cron option or set the value to an empty serialized array.

Using update_option would be safer as I'm not certain as to whether the value should be a serialized empty array or an empty string. You could check in wp-includes/options.php though ... but using update_option will handle it properly without worrying about the database.


Wordpress cron events can also be cleared from the command line, using WP-CLI:

wp cron event list
wp cron event delete your_example_event

More details in the wp-cli docs.

Tags:

Cron