Cache Clearing

MilliCache provides multiple methods to clear cached content, from automatic invalidation to targeted manual clearing.

Automatic Invalidation#Copied!

MilliCache automatically clears cache when content changes:

Post Updates#Copied!

When a post is created, updated, or deleted:

  • The post's URL is cleared
  • Related archives are cleared (category, tag, author, date)
  • The homepage is cleared
  • RSS feeds are cleared

Hooks triggering post-cache clearing:

  • clean_post_cache
  • before_delete_post
  • transition_post_status

Site-Wide Events#Copied!

These events clear the entire site cache:

  • Theme switching (switch_theme)
  • Menu updates (wp_update_nav_menu)
  • Widget updates (widget_update_callback)
  • Customizer saves (customize_save_after)
  • Template part updates (save_post_wp_template_part)
  • Permalink structure changes (update_option_permalink_structure)
  • Active plugins changes (update_option_active_plugins)

Manual Clearing#Copied!

Admin Bar#Copied!

For logged-in users with the publish_pages capability:

  1. Click MilliCache in the admin bar
  2. Select Clear Site Cache (or Clear Network Cache in multisite)
  3. To clear the current page, select Clear Current Page Cache

WP-CLI#Copied!

 1# Clear all cache
 2wp millicache clear
 3
 4# Clear specific posts
 5wp millicache clear --id=1,2,3
 6
 7# Clear by URLs
 8wp millicache clear --url="https://example.com/page-1/,https://example.com/page-2/"
 9
10# Clear by flags
11wp millicache clear --flag="post:123,home,archive:*"
12
13# Clear specific sites (multisite)
14wp millicache clear --site=1,2,3
15
16# Clear entire network (multisite)
17wp millicache clear --network=1

Expire vs Delete#Copied!

By default, wp millicache clear deletes cache entries immediately. Use --expire to mark them as expired instead:

 1wp millicache clear --expire

Expire behavior:

  • Entries remain in cache but marked expired
  • Next request serves stale content (grace)
  • Content regenerates in background
  • Visitors never wait for regeneration

Delete behavior:

  • Entries removed immediately
  • Next request generates fresh content
  • First visitor waits for generation
Tip: Use --expire for non-critical updates to maintain performance. Use deletion for urgent content corrections.

Flag-Based Clearing#Copied!

MilliCache uses flags (tags) to enable targeted cache clearing. Each cached page is tagged with relevant flags.

Built-in Flags#Copied!

Flag Format Applied To Example
home Homepage/blog home
post:{id} Single post/page post:123
archive:post Post archive archive:post
archive:{post_type} Custom Post Type archive:book
archive:{taxonomy}:{term_id} Term archive archive:category:5
archive:author:{id} Author archive archive:author:1
archive:{year} Year archive archive:2026
archive:{year}:{month} Month archive archive:2026:01
feed RSS/Atom feeds feed

Multisite Flag Prefixes#Copied!

In multisite, flags are prefixed with site/network IDs:

Environment Flag Format
Single site post:123
Multisite {site_id}:post:123
Multi-network {network_id}:{site_id}:post:123

Clearing by Flags#Copied!

 1# Clear homepage
 2wp millicache clear --flag="home"
 3
 4# Clear post and its archives
 5wp millicache clear --flag="post:123,archive:post,home"
 6
 7# Clear all category archives
 8wp millicache clear --flag="archive:category:*"

Programmatic Clearing#Copied!

PHP Functions#Copied!

MilliCache provides helper functions in functions.php:

 1// Clear all cache
 2millicache_reset_cache();
 3millicache_reset_cache( true ); // Expire instead of delete
 4
 5// Clear by post-IDs
 6millicache_clear_cache_by_post_ids( [ 1, 2, 3 ] );
 7millicache_clear_cache_by_post_ids( [ 1, 2, 3 ], true ); // Expire
 8
 9// Clear by URLs
10millicache_clear_cache_by_urls( [
11    'https://example.com/page-1/',
12    'https://example.com/page-2/',
13] );
14
15// Clear by flags
16millicache_clear_cache_by_flags( [ 'post:123', 'home' ] );
17millicache_clear_cache_by_flags( [ 'post:123' ], true ); // Expire
18millicache_clear_cache_by_flags( [ 'post:123' ], false, true ); // Add site prefix
19
20// Clear by mixed targets (Post-IDs, Flags & URLs)
21millicache_clear_cache( 'post:123' ); // Single flag
22millicache_clear_cache( [ 1, 'home', 'https://example.com/page-1/' ] ); // Multiple flags
23
24// Multisite: Clear by site IDs
25millicache_clear_cache_by_site_ids( [ 1, 2 ] );
26millicache_clear_cache_by_site_ids( [ 1, 2 ], 1 ); // Specific network
27
28// Multisite: Clear by network
29millicache_clear_cache_by_network_id( 1 );

Hooks for Custom Clearing#Copied!

Clear cache in response to custom events:

 1// Clear cache when a custom option changes
 2add_action( 'update_option_my_custom_option', function() {
 3    millicache_reset_cache();
 4} );
 5
 6// Clear specific posts when the ACF field updates
 7add_action( 'acf/save_post', function( $post_id ) {
 8    millicache_clear_cache_by_post_ids( [ $post_id ] );
 9} );

Custom Flags#Copied!

Add custom flags to enable targeted clearing:

 1// Add a custom flag based on content
 2add_filter( 'millicache_flags_for_request', function( $flags ) {
 3    // Add a flag for WooCommerce shop
 4    if ( function_exists( 'is_shop' ) && is_shop() ) {
 5        $flags[] = 'woo:shop';
 6    }
 7
 8    return $flags;
 9} );

Then clear by your custom flag:

 1wp millicache clear --flag="woo:shop"

Clearing Actions (Hooks)#Copied!

Hook into clearing events:

 1// After clearing by post-IDs
 2add_action( 'millicache_cache_cleared_by_posts', function( $post_ids, $expire ) {
 3    error_log( 'Cleared cache for posts: ' . implode( ', ', $post_ids ) );
 4}, 10, 2 );
 5
 6// After clearing by flags
 7add_action( 'millicache_cache_cleared_by_flags', function( $flags, $expire ) {
 8    // Notify external CDN
 9    notify_cdn_purge( $flags );
10}, 10, 2 );

Best Practices#Copied!

1. Use Targeted Clearing#Copied!

Clear only what's necessary:

 1// Good: Clear specific content
 2millicache_clear_cache_by_post_ids( [ $post_id ] );
 3
 4// Avoid: Clear everything
 5millicache_reset_cache();

2. Prefer Expire Over Delete#Copied!

For non-critical updates, expire instead of delete:

 1millicache_clear_cache_by_post_ids( [ $post_id ], true ); // expire = true

Clear related items together:

 1// Clear post and its relationships
 2$flags = [
 3    "post:{$post_id}",
 4    'home',
 5    'archive:post',
 6];
 7millicache_clear_cache_by_flags( $flags );

4. Use Custom Flags for Cross-Cutting Concerns#Copied!

Add flags for content that should clear together:

 1// Tag all pages showing a promotion
 2add_filter( 'millicache_flags_for_request', function( $flags ) {
 3    if ( is_promotion_active() ) {
 4        $flags[] = 'promo:summer-sale';
 5    }
 6    return $flags;
 7} );
 8
 9// Clear all promotion pages when the sale ends
10millicache_clear_cache_by_flags( [ 'promo:summer-sale' ] );

Next Steps#Copied!