CacheFlush
Provides fine-grained control over Drupal cache clearing through configurable presets that can combine core cache bins, contrib cache tables, and custom cache clearing functions.
cacheflush
Install
composer require 'drupal/cacheflush:^2.0'
Overview
CacheFlush is the ultimate tool for managing Drupal cache clearing with fine granularity. Unlike the standard "clear all caches" approach, this module allows you to create custom presets that clear only specific cache components needed for your particular task.
The module is suitable for developers, administrators, and editors alike. Each preset can combine various cache clearing options including core cache bins (bootstrap, config, render, menu, etc.), contrib module caches, specific cache functions like static reset or Twig cache clearing, and with the advanced submodule, specific cache IDs and cache tags.
Presets can be executed directly from the admin menu, via Drush commands, or automatically through cron jobs. Access to each preset can be controlled through Drupal's permission system, allowing different roles to have access to different cache clearing operations.
Features
- Create unlimited custom cache flush presets combining different cache sources
- Clear core cache bins (bootstrap, config, render, menu, entity, discovery, etc.)
- Clear contrib module cache tables automatically detected from installed modules
- Execute specific cache functions like drupal_static_reset, flush CSS/JS assets, invalidate container, wipe Twig cache, clear plugin caches, and rebuild module/theme data
- Advanced mode for clearing specific cache IDs by bin and invalidating cache tags
- Quick access through admin menu integration with optional per-preset menu entries
- Drush command support for clearing presets from command line
- Cron job integration via Ultimate Cron for scheduled cache clearing
- Views-based administration interface with bulk operations (publish, unpublish, add/remove menu entry, delete)
- Granular access control with separate permissions for viewing, editing, deleting, and executing own vs any presets
- Hooks for custom modules to extend cache clearing options or react to cache clear events
Use Cases
Development: Clear only Twig cache after template changes
Create a preset that only clears the Twig PHP storage cache. This is much faster than clearing all caches when you've only modified .html.twig template files. Select the 'twig' option under 'Other core cache options' and save. Add it to the menu for one-click access during theme development.
Theme development: Reset render and asset caches
Create a preset combining 'render' cache bin, 'asset' (CSS/JS) clearing, and 'twig' cache. This clears everything needed when making frontend changes without rebuilding the entire container or module data.
Plugin development: Clear discovery and plugin caches
Create a preset with 'discovery' cache bin and 'plugin' option. Use this when developing custom plugins (blocks, fields, etc.) to quickly recognize new or modified plugin definitions without a full cache rebuild.
Content editor: Clear specific page cache by cache tags
Using the Advanced submodule, create a preset that invalidates specific cache tags like 'node:123' or 'taxonomy_term:45'. This allows editors to clear the cache for specific content without affecting the rest of the site.
Scheduled cache maintenance with cron
Enable the Cron submodule and create a preset that clears the render cache. Enable cron for this preset and configure the Ultimate Cron job to run every hour during low-traffic periods. This ensures render cache doesn't grow unbounded on high-traffic sites.
CI/CD pipeline cache clearing via Drush
Use the Drush submodule to integrate cache clearing into deployment scripts. Create presets for different deployment scenarios (code-only deploys might just need container invalidation, while feature deployments might need router rebuilding) and call them via 'drush cf [id]' in your deployment pipeline.
Tips
- Start with a preset that clears only what you commonly need during development - most tasks don't require a full cache rebuild
- Add frequently-used presets to the admin menu for one-click access from anywhere in the admin interface
- Use the Advanced submodule's cache tags feature to invalidate content-specific caches without affecting anonymous page caching
- When debugging, create a 'Nuclear Option' preset that clears everything, but use specific presets for daily work to save time
- Consider preset naming conventions like 'Dev: Twig only' or 'Deploy: Full rebuild' to make their purpose clear in the menu
Technical Details
Admin Pages 6
/admin/cacheflush
Main administration landing page for CacheFlush module. Provides access to the Clear All function and links to individual cache flush presets that have been added to the menu.
/admin/structure/cacheflush
Views-based listing of all cacheflush preset entities. Displays preset ID, title, author, status (published/unpublished), menu entry status, and changed date. Includes bulk operations and filters for status and menu entry.
/admin/structure/cacheflush/add
Form for creating a new cache flush preset. Organize cache sources into categorized vertical tabs: Core cache tables, Other core cache options (functions), Contrib cache tables, and Other contrib cache options.
/cacheflush/{cacheflush}/edit
Form for editing an existing cache flush preset. Same structure as the add form with current values pre-populated.
/cacheflush/{cacheflush}/delete
Confirmation form for deleting a cache flush preset.
/admin/structure/cacheflush/settings
Settings form for Cacheflush entity configuration. Currently provides placeholder for managing field settings.
Permissions 11
Hooks 4
hook_cacheflush_tabs_options
Define custom cache clearing options that appear in the preset creation form. Each option specifies a description, category tab, and functions to execute.
hook_cacheflush_ui_tabs
Define custom tabs/categories in the preset creation form to organize cache clearing options.
hook_cacheflush_before_clear
Executes before a cache flush preset is cleared. Allows modules to perform actions or logging before cache clearing.
hook_cacheflush_after_clear
Executes after a cache flush preset has been cleared. Allows modules to perform cleanup or notification actions.
Drush Commands 1
drush cacheflush [id]
Clear cache using a predefined cacheflush preset. Without an ID argument, lists all available published presets.
Troubleshooting 5
Ensure the preset is both Published (status) AND has 'Menu entry' enabled. Check these values in the entity list at /admin/structure/cacheflush. Also verify you have 'cacheflush clear cache' permission.
The preset must be published to be executable. Edit the preset and ensure it is saved with published status, or use the bulk operations to publish it.
Ensure the Ultimate Cron module is installed and enabled before enabling the cacheflush_cron submodule. If you enabled cacheflush_cron first, disable and re-enable it after installing Ultimate Cron.
Contrib modules must implement hook_cacheflush_tabs_options() to expose their cache clearing options. If a module's cache option doesn't appear, the module may not support CacheFlush integration.
Ensure you've selected both a Cache ID and a Service (cache bin). Both fields are required. The cache ID must exactly match the cid used when storing the cache item.
Security Notes 3
- The 'cacheflush clear cache' permission should only be granted to trusted roles as cache clearing can temporarily impact site performance
- The 'cacheflush administer' permission allows creating presets that could clear critical caches - assign carefully
- Presets can execute arbitrary functions defined via hooks - review any contributed modules implementing hook_cacheflush_tabs_options()