wppaste
WordPress

delete_metadata( string $meta_type, int $object_id, string $meta_key, mixed $meta_value = '', bool $delete_all = false ): bool

Since
2.9.0
Source
wp-includes/meta.php:386
Deletes metadata for the specified object.

Compatibility

WordPress
since 2.9.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

$meta_typestring
Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.
$object_idint
ID of the object metadata is for.
$meta_keystring
Metadata key.
$meta_valuemixedoptional
Metadata value. Must be serializable if non-scalar.
If specified, only delete metadata entries with this value.
Otherwise, delete all entries with the specified meta_key.
Pass null, false, or an empty string to skip this check.
(For backward compatibility, it is not possible to pass an empty string to delete those entries with an empty string for a value.) Default empty string.Default: ''
$delete_allbooloptional
If true, delete matching metadata entries for all objects, ignoring the specified object_id. Otherwise, only delete matching metadata entries for the specified object_id. Default false.Default: false

Return value

bool
True on successful delete, false on failure.

Performance profile

How much work a call to delete_metadata() 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

Reaches the database via ->get_col().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8–172

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

Plugin surface
5 hooks

Third-party callbacks on 'delete_{$meta_type}_metadata', 'delete_{$meta_type}_meta', 'delete_postmeta' run inside this call, and their cost is not bounded by anything here.

Called by
14

14 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
  • serializeserialisationmaybe_serialize()called directly
  • cacheobject cachewp_cache_delete_multiple()called directly
  • sqldatabase query->get_col()called directly

What one call costs · 43 distinct outcomes

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

WhenInstructionsCalls it makes
always8–12none
always17–18absint()
always21–23absint(), _get_meta_table()
$check !== null53–56absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters()
$check === null && !$meta_ids76–83absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col()
$check === null && !$meta_ids84–91absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && !$meta_ids93–96absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col()
$check === null && $meta_ids && $meta_type !== "post"103–110absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), do_action(), ->query()
$check === null && $meta_ids && $meta_type === "post"107–114absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), do_action(), do_action(), ->query()
$check === null && $meta_ids && $meta_type !== "post"111–118absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), do_action(), ->query()
$check === null && $meta_ids && $meta_type === "post"115–122absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), do_action(), do_action(), ->query()
$check === null && $meta_ids && $meta_type !== "post"118–131absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), ->query()
31 further outcomes, up to 172 instructions
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids && $meta_type !== "post"120–123absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), do_action(), ->query()
$check === null && $meta_ids && $meta_type !== "post"122–130absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_ids && $meta_type === "post"122–135absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), do_action(), ->query()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids && $meta_type === "post"124–127absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), do_action(), do_action(), ->query()
$check === null && $meta_ids126–134absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_ids126–134absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_ids && $meta_type !== "post"126–139absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), ->query()
$check === null && $meta_ids && $meta_type !== "post"130–138absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_ids && $meta_type === "post"130–138absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_ids && $meta_type === "post"130–143absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), do_action(), ->query()
$check === null && $meta_ids134–142absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_ids134–142absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids && $meta_type !== "post"135–144absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), ->query()
$check === null && $meta_ids && $meta_type !== "post"137–151absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_ids && $meta_type === "post"138–146absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids && $meta_type !== "post"139–143absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids && $meta_type === "post"139–148absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), do_action(), ->query()
$check === null && $meta_ids141–155absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_ids141–155absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids143–147absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids143–147absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_ids && $meta_type !== "post"145–159absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_ids && $meta_type === "post"145–159absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids && $meta_type === "post"147–151absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_ids149–163absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_ids149–163absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_ids && $meta_type === "post"153–167absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids && $meta_type !== "post"154–164absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids158–168absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids158–168absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action()
$check === null && $meta_value !== "" && $meta_value !== null && $meta_value !== false && $meta_ids && $meta_type === "post"162–172absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->prepare(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), do_action(), do_action(), ->query(), wp_cache_delete_multiple(), do_action(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1948–17222
8.51948–17222
8.41948–172225 fewer instructions than PHP 8.3
8.31998–17722
8.21998–17722
8.11998–17722
7.41998–17722

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 · 5

5 hooks fire while delete_metadata() runs, in this order:

  1. apply_filters( delete_{$meta_type}_metadata )filterline 434 (+48 into the body)

    Short-circuits deleting metadata of a specific type.

  2. do_action( delete_{$meta_type}_meta )actionline 485 (+99 into the body)

    Fires immediately before deleting metadata of a specific type.

  3. do_action( delete_postmeta )actionline 496 (+110 into the body)

    Fires immediately before deleting metadata for a post.

  4. do_action( deleted_{$meta_type}_meta )actionline 534 (+148 into the body)

    Fires immediately after deleting metadata of a specific type.

  5. do_action( deleted_postmeta )actionline 545 (+159 into the body)

    Fires immediately after deleting metadata for a post.

Uses · 8

Used by · 14

Show all 14

Source code

function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {	global $wpdb; 	if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {		return false;	} 	$object_id = absint( $object_id );	if ( ! $object_id && ! $delete_all ) {		return false;	} 	$table = _get_meta_table( $meta_type );	if ( ! $table ) {		return false;	} 	$type_column = sanitize_key( $meta_type . '_id' );	$id_column   = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 	// expected_slashed ($meta_key)	$meta_key   = wp_unslash( $meta_key );	$meta_value = wp_unslash( $meta_value ); 	/**	 * Short-circuits deleting metadata of a specific type.	 *	 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type	 * (post, comment, term, user, or any other type with an associated meta table).	 * Returning a non-null value will effectively short-circuit the function.	 *	 * Possible hook names include:	 *	 *  - `delete_post_metadata`	 *  - `delete_comment_metadata`	 *  - `delete_term_metadata`	 *  - `delete_user_metadata`	 *	 * @since 3.1.0	 *	 * @param null|bool $delete     Whether to allow metadata deletion of the given type.	 * @param int       $object_id  ID of the object metadata is for.	 * @param string    $meta_key   Metadata key.	 * @param mixed     $meta_value Metadata value. Must be serializable if non-scalar.	 * @param bool      $delete_all Whether to delete the matching metadata entries	 *                              for all objects, ignoring the specified $object_id.	 *                              Default false.	 */	$check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );	if ( null !== $check ) {		return (bool) $check;	} 	$_meta_value = $meta_value;	$meta_value  = maybe_serialize( $meta_value ); 	$query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key ); 	if ( ! $delete_all ) {		$query .= $wpdb->prepare( " AND $type_column = %d", $object_id );	} 	if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {		$query .= $wpdb->prepare( ' AND meta_value = %s', $meta_value );	} 	$meta_ids = $wpdb->get_col( $query );	if ( ! count( $meta_ids ) ) {		return false;	} 	if ( $delete_all ) {		if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {			$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) );		} else {			$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );		}	} 	/**

Changelog

Introduced in 2.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.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/meta.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.