"You know, hope is a mistake. If you can't fix what's broken, you'll, uh... you'll go insane." - Max Rockatansky
Magento 2 Cache Clean
A faster drop in replacement for
bin/magento cache:clean with a file watcher.
The file watcher automatically cleans affected cache types in the Magento 2
cache during development.
For example, if you make a change to a template, it only cleans the
full_page caches, not the
The project is only tested on MacOS and Linux. Please report bugs by opening an issue on the GitHub issue tracker.
- Supports file, redis and varnish cache backends
- Removes affected generated code classes when a source file is changed
- Zero configuration, all required information is read from
- Hotkeys for quick cache flushes while the watcher is running
Installation & Updating
composer require --dev mage2tv/magento-cache-clean
composer remove --dev mage2tv/magento-cache-clean composer require --dev mage2tv/magento-cache-clean
In your Magento directory, run
Ctrl-C to exit the watcher process.
The script can be used as a faster drop in replacement of
too. For example:
vendor/bin/cache-clean.js config full_page
(It's quicker because the start up time of
bin/magento is so slow.)
There are several options to customize the behavior:
vendor/bin/cache-clean.js --help Sponsored by https://www.mage2.tv Usage: cache-clean.js [options and flags] [cache-types...] Clean the given cache types. If none are given, clean all cache types. --directory|-d <dir> Magento base directory --watch|-w Watch for file changes --verbose|-v Display more information --debug|-vv Display too much information --silent|-s Display less information --version Display the version --help|-h This help message
Usually I run the command once with the
--watch switch when I start
development, and when I make a change that isn't automatically detected (yet),
vendor/bin/cache-clean.js with the given cache types as a drop in
When the watcher is running, segments of the cache can be cleaned with individual keystrokes:
||(a for all)|
||(v for view)
node.js(built on 10.8, but should work with older 8.x versions, too).
- it probably is a good idea to turn on all Magento caches
bin/magento cache:enableto get the full benefit.
This utility aims to improve the Magento developer experience by shortening the feedback loop during development through automating the removal of affected cache sections after file changes.
- Magento uses caching a lot and is faster when the caches are warm.
- As a developer I want a quick feedback loop.
- Rebuilding the cache takes longer than cleaning the cache
To support the above assumptions, I want to only clean the cache segments I really have to after making some changes.
For example, if I make a change to a template, I only want to flush the
full_page caches, not the
Thinking about what cache types need to be cleaned after a change and typing the exact command takes time, and it also gets very repetitive, so many developers simply nuke the whole cache after every change.
Automating selective cache cleaning improves the developer experience.
Currently the watcher has to be restarted after a new theme is added so it is added to the watchlist.
Changes to files on NFS mounts (e.g. in vagrant) do not trigger the watches. Depending on a given setup, it might be possible to run the watcher on the host system instead.
Not tested on Windows, please open an issue if you want to contribute.
If you run into the error
Error NOSPCon Linux, run the command:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
The tool is written in ClojureScript.
To build, install Clojure 1.9 or later (e.g.
brew install clojure) and run
$ clj -m figwheel.main -O advanced -bo build $ chmod +x bin/cache-clean.js
Thanks to Mage2 TV for sponsoring the development of this tool.
This script was inspired by Timon de Groot's
blog post where he
describes the idea to use a file watcher in PHPStorm to call
clear the complete cache whenever a XML file is modified.
The only downside of that solution is that it always flushes the full cache and
only works with redis.
Copyright & License
Copyright 2018 by Vinai Kopp, distributed under the BSD-3-Clause license (see the LICENSE file in this repository).