wppaste
WordPress

wp_unschedule_hook( string $hook, bool $wp_error = false ): int|false|WP_Error

Since
4.9.0, 5.1.0, 5.7.0
Source
wp-includes/cron.php:654
Unschedules all events attached to the hook.

Description

Can be useful for plugins when deactivating to clean up the cron queue.

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.
$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 on the hook), false or WP_Error if unscheduling fails.

Performance profile

How much work a call to wp_unschedule_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
16–33

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

Plugin surface
1 hook

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

Called by
1

1 place 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
  • optionoption read or writeget_option()one call below wp_unschedule_hook()

Further down the call graph this can also reach cache, serialize, 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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
$pre !== null && $pre !== false16apply_filters()
$pre === null17–23apply_filters(), _get_cron_array()
$pre !== null17–20apply_filters(), is_wp_error()
$pre !== null && $pre === false22apply_filters(), __()
$pre === null && !empty($crons) && !empty($results) && $set !== true29–30apply_filters(), _get_cron_array(), _set_cron_array()
$pre === null && !empty($crons) && !empty($results) && $set === true32–33apply_filters(), _get_cron_array(), _set_cron_array(), array_sum()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 69 instructions, 16–33 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_unschedule_hook() runs, in this order:

  1. apply_filters( pre_unschedule_hook )filterline 673 (+19 into the body)

    Filter to override clearing all events attached to the hook.

Uses · 6

Used by · 1

Source code

function wp_unschedule_hook( $hook, $wp_error = false ) {	/**	 * Filter to override clearing all events attached to the 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). If unscheduling	 * one or more events fails then return either a WP_Error object or false depending	 * on the value of the `$wp_error` parameter.	 *	 * @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 hook.	 * @param string                  $hook     Action hook, the execution of which will be unscheduled.	 * @param bool                    $wp_error Whether to return a WP_Error on failure.	 */	$pre = apply_filters( 'pre_unschedule_hook', null, $hook, $wp_error ); 	if ( null !== $pre ) {		if ( $wp_error && false === $pre ) {			return new WP_Error(				'pre_unschedule_hook_false',				__( 'A plugin prevented the hook from being cleared.' )			);		} 		if ( ! $wp_error && is_wp_error( $pre ) ) {			return false;		} 		return $pre;	} 	$crons = _get_cron_array();	if ( empty( $crons ) ) {		return 0;	} 	$results = array(); 	foreach ( $crons as $timestamp => $args ) {		if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) {			$results[] = count( $crons[ $timestamp ][ $hook ] );		} 		unset( $crons[ $timestamp ][ $hook ] ); 		if ( empty( $crons[ $timestamp ] ) ) {			unset( $crons[ $timestamp ] );		}	} 	/*	 * If the results are empty (zero events to unschedule), no attempt	 * to update the cron array is required.	 */	if ( empty( $results ) ) {		return 0;	} 	$set = _set_cron_array( $crons, $wp_error ); 	if ( true === $set ) {		return array_sum( $results );	} 	return $set;}

Changelog

Introduced in 4.9.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 added to indicate success or failure.from the docblock
4.9.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.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.