admin管理员组文章数量:1122832
I discovered that I have 29,000 cron jobs in my WordPress database from deactivated and deleted plugins. I have tried a number of optimizer plugins but the huge number of cron jobs means I can't delete them using plugins.
I also tried this in my functions.php without success:
add_action("init", "clear_crons_left");
function clear_crons_left() {
wp_clear_scheduled_hook("cron_name");
}
Is there any SQL command I can use in phpmyadmin to search by cron hook and remove them?
I discovered that I have 29,000 cron jobs in my WordPress database from deactivated and deleted plugins. I have tried a number of optimizer plugins but the huge number of cron jobs means I can't delete them using plugins.
I also tried this in my functions.php without success:
add_action("init", "clear_crons_left");
function clear_crons_left() {
wp_clear_scheduled_hook("cron_name");
}
Is there any SQL command I can use in phpmyadmin to search by cron hook and remove them?
Share Improve this question edited Jan 24, 2015 at 21:10 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Jan 24, 2015 at 18:48 Pádraig Ó BeirnPádraig Ó Beirn 4811 gold badge3 silver badges9 bronze badges 1- I found WP Bulk delete, use with carefull and put a max of items to delete at once – Zwelly Commented Nov 7, 2019 at 11:05
11 Answers
Reset to default 20Thanks 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! :-)
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.
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.
An even simpler solution is to call delete_option( 'cron' );
once in some plugin. All automatically added cron jobs will get added again on the next visit/request of your site.
As a one case (mu) plugin that only runs whenever you activate it:
<?php
/** Plugin Name: Clean Cron */
register_activation_hook( __FILE__, function()
{
delete_option( 'cron' );
} );
In case someone wanted to clear a specific cron name (say 'CRON_NAME'), this solution worked for me:
$crons = _get_cron_array();
//echo "Found total ".count($crons)."<br />";
//Keep only the ones that don't match the cron name
$updated = array_filter($crons, function($v){return !array_key_exists("CRON_NAME",$v);});
//echo "Reduced to ".count($updated)."<br />";
_set_cron_array($updated);
I had an year full of pending cron jobs, about 5 Mb data for this single database entry. Deleted the cron jobs from the database. Disabled cron jobs in wp-config.php
Set up a manual cron job in cpanel. Now my site is literally flying. I had been upgrading servers, buying more CPU/RAM, but all was a waste of money and time.
To delete all pending cron jobs run this query in phpmyadmin>Run query:
UPDATE wp_options SET option_value = '' WHERE option_name = 'cron'
Thanks a lot Pádraig Ó Beirn.
I ran into a similar issue, where because of one of my own coding errors, thousands of copies of one particular cron job had been added to a site. The wp_clear_scheduled_hook function appeared to time out and fail. I got around it with a script that unset all instances of the cron function within the array and then adds the filtered array as the new cron option in the options table. See below.
In this way, I avoided blowing away the desirable cron jobs previously added to the site.
This could be modified as a function that takes an array of handles to eliminate or an array of handles to be preserved.
$crons = _get_cron_array();
$hook = 'tj_flush_w3tc_cache';
foreach ( $crons as $timestamp => $cron ) {
if ( isset( $cron[ $hook ] ) ) {
unset($cron[$hook]);
}
if(!empty($cron))
$newcron[$timestamp] = $cron;
}
update_option('cron',$newcron);
I have a way very simple to delete all cron events. Before, you need to DISABLE WP Cron in wp-config Then, you install Plugin WP Control Then, Move to Tool menu > Cron events > Click chose all > Delete all of them. Could you try it. Thanks.
Run this from cli, it uses wp-cli, it basically goes through all of them and deletes them one by one.
wp cron event list --fields=hook | tail -n +2 | xargs -I {} wp cron event delete {}
If you clear your cron tasks this way and you use UpdraftPlus, you will need to re-save your settings in order to regenerate the cron tasks. Until you do this, your automated backups will not run (but manual backups will).
The settings will still be there, and you don't need to edit anything. Just go to [UpdraftPlus top menu]->Settings, and the scroll down to the bottom and click "Save Changes".
I got here because of the huge amount of sm_ping
cronjobs in wp_options
. If that is your problem, you could try the following :
Put this in functions.php (child theme) if you don't have access to phpmyadmin, especially if your site is bloated with ping cronjobs (sm_ping) :
if (isset($_GET['doing_wp_cron'])) {
remove_action('do_pings', 'do_all_pings');
wp_clear_scheduled_hook('do_pings');
}
本文标签: Delete thousands of cron jobs
版权声明:本文标题:Delete thousands of cron jobs 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305444a1932592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论