Wordpress - Delete all scheduled events with a particular hook

I have just quickly wrote the below function, it will clear all crons for the specified hook, irrespective of the cron time and the hook argument.

NOTE: I have NOT TESTED the function, so please don't run it on your live site.

function wpse39681_clear_all_crons( $hook ) {
    $crons = _get_cron_array();
    if ( empty( $crons ) ) {
        return;
    }
    foreach( $crons as $timestamp => $cron ) {
        if ( ! empty( $cron[$hook] ) )  {
            unset( $crons[$timestamp][$hook] );
        }

        if ( empty( $crons[$timestamp] ) ) {
            unset( $crons[$timestamp] );
        }
    }
    _set_cron_array( $crons );
}

Related: http://core.trac.wordpress.org/ticket/18997


4.9.0 introduced wp_unschedule_hook()

https://developer.wordpress.org/reference/functions/wp_unschedule_hook/

Tags:

Cron