wp_unschedule_event()WP 2.1.0

Deletes the specified cron action from the schedule. To delete, you need to specify all the data: timestamp, hook name, and parameters.

To cancel the action, you must know the exact time it is scheduled for and the arguments that should be passed to the function.

More about deleting cron tasks: WP Cron (scheduler) in WordPress

The cron action is added via wp_schedule_event().

Hooks from the function

Returns

true|false|WP_Error. Returns false if the parameter $timestamp is incorrectly specified.

Usage

wp_unschedule_event( $timestamp, $hook, $args, $wp_error );
$timestamp(string) (required)
Timestamp in UNIX format (321546564) at which the action we want to delete is supposed to occur.
$hook(string) (required)
The name of the hook to which the function performing the action at the specified $timestamp time is "attached".
$args(array)
Array of arguments that should be passed to the function.
Default: array()
$wp_error(true/false) (WP 5.7)
true - will return a WP_Error object on failure.
Default: false

Examples

1

#1 Remove (Cancel) the CRON Job with the parameters

If you have specified parameters for a cron-task when registering it, you must specify those parameters in order to delete such a task, otherwise the event will not be deleted.

wp_unschedule_event( 1424106123, 'my_schedule_hook', array('id'=>654) );
0

#2 Remove (Cancel) the WP CRON Task

It is assumed that the function attached to the hook does not pass any data, so $args need not be specified.

$timestamp = wp_next_scheduled( 'my_schedule_hook' ); //> 1424106123

wp_unschedule_event( $timestamp, 'my_schedule_hook' );

1424106123 - is a time when task need to be executed.

See also: wp_next_scheduled().

You can also use wp_clear_scheduled_hook(), which calls wp_unschedule_event(). It will similarly clear all future events and save you from calling wp_next_scheduled() yourself.

Changelog

Since 2.1.0 Introduced.
Since 5.1.0 Return value modified to boolean indicating success or failure, pre_unschedule_event filter added to short-circuit the function.
Since 5.7.0 The $wp_error parameter was added.

wp_unschedule_event() code WP 7.0

function wp_unschedule_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
	// Make sure timestamp is a positive integer.
	if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
		if ( $wp_error ) {
			return new WP_Error(
				'invalid_timestamp',
				__( 'Event timestamp must be a valid Unix timestamp.' )
			);
		}

		return false;
	}

	/**
	 * Filter to override unscheduling of events.
	 *
	 * Returning a non-null value will short-circuit the normal unscheduling
	 * process, causing the function to return the filtered value instead.
	 *
	 * For plugins replacing wp-cron, return true if the event was successfully
	 * unscheduled, false or a WP_Error if not.
	 *
	 * @since 5.1.0
	 * @since 5.7.0 The `$wp_error` parameter was added, and a WP_Error object can now be returned.
	 *
	 * @param null|bool|WP_Error $pre       Value to return instead. Default null to continue unscheduling the event.
	 * @param int                $timestamp Unix timestamp (UTC) for when to run the event.
	 * @param string             $hook      Action hook, the execution of which will be unscheduled.
	 * @param array              $args      Arguments to pass to the hook's callback function.
	 * @param bool               $wp_error  Whether to return a WP_Error on failure.
	 */
	$pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args, $wp_error );

	if ( null !== $pre ) {
		if ( $wp_error && false === $pre ) {
			return new WP_Error(
				'pre_unschedule_event_false',
				__( 'A plugin prevented the event from being unscheduled.' )
			);
		}

		if ( ! $wp_error && is_wp_error( $pre ) ) {
			return false;
		}

		return $pre;
	}

	$crons = _get_cron_array();
	$key   = md5( serialize( $args ) );

	unset( $crons[ $timestamp ][ $hook ][ $key ] );

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

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

	return _set_cron_array( $crons, $wp_error );
}