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:380
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.
Passnull,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
- Scaling
- Constant
- Instructions
- 8–172
- Plugin surface
- 5 hooks
- Called by
- 14
Reaches the database via ->get_col().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 194.
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.
14 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - serializeserialisation
maybe_serialize()called directly - cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–12 | none |
| always | 17–18 | absint() |
| always | 21–23 | absint(), _get_meta_table() |
$check !== null | 53–56 | absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters() |
$check === null && !$meta_ids | 76–83 | absint(), _get_meta_table(), sanitize_key(), wp_unslash(), wp_unslash(), apply_filters(), maybe_serialize(), ->prepare(), ->get_col() |
$check === null && !$meta_ids | 84–91 | absint(), _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_ids | 93–96 | absint(), _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–110 | absint(), _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–114 | absint(), _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–118 | absint(), _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–122 | absint(), _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–131 | absint(), _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–123 | absint(), _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–130 | absint(), _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–135 | absint(), _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–127 | absint(), _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_ids | 126–134 | absint(), _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_ids | 126–134 | absint(), _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–139 | absint(), _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–138 | absint(), _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–138 | absint(), _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–143 | absint(), _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_ids | 134–142 | absint(), _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_ids | 134–142 | absint(), _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–144 | absint(), _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–151 | absint(), _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–146 | absint(), _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–143 | absint(), _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–148 | absint(), _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_ids | 141–155 | absint(), _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_ids | 141–155 | absint(), _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_ids | 143–147 | absint(), _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_ids | 143–147 | absint(), _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–159 | absint(), _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–159 | absint(), _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–151 | absint(), _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_ids | 149–163 | absint(), _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_ids | 149–163 | absint(), _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–167 | absint(), _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–164 | absint(), _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_ids | 158–168 | absint(), _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_ids | 158–168 | absint(), _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–172 | absint(), _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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 194 | 8–172 | 22 | |
| 8.5 | 194 | 8–172 | 22 | |
| 8.4 | 194 | 8–172 | 22 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 199 | 8–177 | 22 | |
| 8.2 | 199 | 8–177 | 22 | |
| 8.1 | 199 | 8–177 | 22 | |
| 7.4 | 199 | 8–177 | 22 |
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:
- apply_filters( delete_{$meta_type}_metadata )filterline 428 (+48 into the body)
Short-circuits deleting metadata of a specific type.
- do_action( delete_{$meta_type}_meta )actionline 479 (+99 into the body)
Fires immediately before deleting metadata of a specific type.
- do_action( delete_postmeta )actionline 490 (+110 into the body)
Fires immediately before deleting metadata for a post.
- do_action( deleted_{$meta_type}_meta )actionline 528 (+148 into the body)
Fires immediately after deleting metadata of a specific type.
- do_action( deleted_postmeta )actionline 539 (+159 into the body)
Fires immediately after deleting metadata for a post.
Uses · 8
- absint()Converts a value to non-negative integer.
- _get_meta_table()Retrieves the name of the metadata table for the specified object type.
- sanitize_key()Sanitizes a string key.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- maybe_serialize()Serializes data, if needed.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_cache_delete_multiple()Deletes multiple values from the cache in one call.
Used by · 14
- WP_REST_Meta_Fields::delete_meta_value()Deletes a meta value for an object.
- WP_REST_Meta_Fields::update_multi_meta_value()Updates multiple meta values for an object.
- WP_User_Meta_Session_Tokens::drop_sessions()Destroys all sessions for all users.
- delete_comment_meta()Removes metadata matching criteria from a comment.
- delete_post_meta()Deletes a post meta field for the given post ID.
- delete_post_meta_by_key()Deletes everything from post meta matching the given meta key.
- delete_site_meta()Removes metadata matching criteria from a site.
- delete_site_meta_by_key()Deletes everything from site meta matching meta key.
- delete_term_meta()Removes metadata matching criteria from a term.
- delete_user_meta()Removes metadata matching criteria from a user.
- install_blog()Install an empty blog.
- wp_autosave_post_revisioned_meta_fields()Autosaves the revisioned meta fields.
Show all 14
- wp_delete_attachment()Trashes or deletes an attachment.
- wp_initialize_site()Runs the initialization routine for a given site.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.