clean_attachment_cache( int $id, bool $clean_terms = false )
- Since
- 3.0.0
- Source
wp-includes/post.php:7888
Description
Cleaning means delete from the cache. Optionally will clean the term object cache associated with the attachment ID.
This function will not run if $_wp_suspend_cache_invalidation is not empty.
Compatibility
- WordPress
- since 3.0.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
$idint- The attachment ID in the cache to clean.
$clean_termsbooloptional- Whether to clean terms cache. Default false.Default:
false
Performance profile
How much work a call to clean_attachment_cache() 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
- Light
- Scaling
- Constant
- Instructions
- 6–25
- Plugin surface
- 1 hook
- Called by
- 1
Only touches the object cache, via wp_cache_delete().
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 26.
Third-party callbacks on 'clean_attachment_cache' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_delete()called directly - hookthird-party callbacks
do_action()called directly
Further down the call graph this can also reach query, serialize, option 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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost clean_attachment_cache() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($_wp_suspend_cache_invalidation) | 6 | none |
empty($_wp_suspend_cache_invalidation) | 21 | wp_cache_delete(), wp_cache_delete(), do_action() |
empty($_wp_suspend_cache_invalidation) | 25 | wp_cache_delete(), wp_cache_delete(), clean_object_term_cache(), do_action() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 26 instructions, 6–25 executed per call, 2 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 clean_attachment_cache() runs, in this order:
- do_action( clean_attachment_cache )actionline 7911 (+23 into the body)
Fires after the given attachment's cache is cleaned.
Uses · 3
- wp_cache_delete()Removes the cache contents matching key and group.
- clean_object_term_cache()Removes the taxonomy relationship to terms from the cache.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 1
- wp_media_attach_action()Encapsulates the logic for Attach/Detach actions.
Source code
function clean_attachment_cache( $id, $clean_terms = false ) { global $_wp_suspend_cache_invalidation; if ( ! empty( $_wp_suspend_cache_invalidation ) ) { return; } $id = (int) $id; wp_cache_delete( $id, 'posts' ); wp_cache_delete( $id, 'post_meta' ); if ( $clean_terms ) { clean_object_term_cache( $id, 'attachment' ); } /** * Fires after the given attachment's cache is cleaned. * * @since 3.0.0 * * @param int $id Attachment ID. */ do_action( 'clean_attachment_cache', $id );}Changelog
Introduced in 3.0.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 7.0.4 tag, from
src/wp-includes/post.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.