wppaste
WordPress

wp_clear_scheduled_hook( string $hook, array $args = array(), bool $wp_error = false ): int|false|WP_Error

Since
2.1.0, 5.1.0, 5.7.0
Source
wp-includes/cron.php:549
Unschedules all events attached to the hook with the specified arguments.

Description

Warning: This function may return boolean false, but may also return a non-boolean value which evaluates to false. For information about casting to booleans see the https://www.php.net/manual/en/language.types.boolean.php PHP documentation. Use the === operator for testing the return value of this function.

Compatibility

WordPress
since 5.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$hookstring
Action hook, the execution of which will be unscheduled.
$argsarrayoptional
Array containing each separate argument to pass to the hook's callback function.
Although not passed to a callback, these arguments are used to uniquely identify the event, so they should be the same as those used when originally scheduling the event.
Default empty array.Default: array()
$wp_errorbooloptional
Whether to return a WP_Error on failure. Default false.Default: false

Return value

int|false|WP_Error
On success an integer indicating number of events unscheduled (0 indicates no events were registered with the hook and arguments combination), false or WP_Error if unscheduling one or more events fail.

Performance profile

How much work a call to wp_clear_scheduled_hook() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
20–59

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 94.

Plugin surface
1 hook

Third-party callbacks on 'pre_clear_scheduled_hook' run inside this call, and their cost is not bounded by anything here.

Called by
8

8 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • serializeserialisationserialize()called directly
  • optionoption read or writeget_option()one call below wp_clear_scheduled_hook()

Further down the call graph this can also reach cache, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_clear_scheduled_hook() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_array($args) && $pre !== null && $pre !== false20apply_filters()
is_array($args) && $pre === null && empty($crons)21apply_filters(), _get_cron_array()
is_array($args) && $pre !== null21–24apply_filters(), is_wp_error()
is_array($args) && $pre !== null && $pre === false26apply_filters(), __()
!is_array($args) && $pre !== null && $pre !== false31__(), _deprecated_argument(), apply_filters()
!is_array($args) && $pre === null && empty($crons)32__(), _deprecated_argument(), apply_filters(), _get_cron_array()
!is_array($args) && $pre !== null32–35__(), _deprecated_argument(), apply_filters(), is_wp_error()
!is_array($args) && $pre !== null && $pre === false37__(), _deprecated_argument(), apply_filters(), __()
is_array($args) && $pre === null && !empty($crons)41–48apply_filters(), _get_cron_array(), serialize(), md5(), array_filter()
!is_array($args) && $pre === null && !empty($crons)52–59__(), _deprecated_argument(), apply_filters(), _get_cron_array(), serialize(), md5(), array_filter()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 94 instructions, 20–59 executed per call, 12 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_clear_scheduled_hook() runs, in this order:

  1. apply_filters( pre_clear_scheduled_hook )filterline 583 (+34 into the body)

    Filter to override clearing a scheduled hook.

Uses · 7

Used by · 8

Source code

function wp_clear_scheduled_hook( $hook, $args = array(), $wp_error = false ) {	/*	 * Backward compatibility.	 * Previously, this function took the arguments as discrete vars rather than an array like the rest of the API.	 */	if ( ! is_array( $args ) ) {		_deprecated_argument(			__FUNCTION__,			'3.0.0',			__( 'This argument has changed to an array to match the behavior of the other cron functions.' )		); 		$args     = array_slice( func_get_args(), 1 ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection		$wp_error = false;	} 	/**	 * Filter to override clearing a scheduled hook.	 *	 * 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 the number of events successfully	 * unscheduled (zero if no events were registered with the hook) or false	 * or a WP_Error if unscheduling one or more events fails.	 *	 * @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|int|false|WP_Error $pre      Value to return instead. Default null to continue unscheduling 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_clear_scheduled_hook', null, $hook, $args, $wp_error ); 	if ( null !== $pre ) {		if ( $wp_error && false === $pre ) {			return new WP_Error(				'pre_clear_scheduled_hook_false',				__( 'A plugin prevented the hook from being cleared.' )			);		} 		if ( ! $wp_error && is_wp_error( $pre ) ) {			return false;		} 		return $pre;	} 	/*	 * This logic duplicates wp_next_scheduled().	 * It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,	 * and, wp_next_scheduled() returns the same schedule in an infinite loop.	 */	$crons = _get_cron_array();	if ( empty( $crons ) ) {		return 0;	} 	$results = array();	$key     = md5( serialize( $args ) ); 	foreach ( $crons as $timestamp => $cron ) {		if ( isset( $cron[ $hook ][ $key ] ) ) {			$results[] = wp_unschedule_event( $timestamp, $hook, $args, true );		}	} 	$errors = array_filter( $results, 'is_wp_error' );	$error  = new WP_Error(); 	if ( $errors ) {		if ( $wp_error ) {			array_walk( $errors, array( $error, 'merge_from' ) ); 			return $error;		}

Changelog

Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.7.0
The $wp_error parameter was added.from the docblock
5.1.0
Return value modified to indicate success or failure, 'pre_clear_scheduled_hook' filter added to short-circuit the function.from the docblock
2.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/cron.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.