Is it possible to hook on drush cache:rebuild before the command is executed? I've seen some hooks for afterwards, but I am trying to compile my less files into css before cache the is rebuild. I'd be fine with it simply being just for the drush cr command in terminal, any way to automate the compiling on cache rebuild would be great!

All of the modules I've found for compiling less files requires you to do it manually, but not whenever cache is rebuilt like in D7.

Any ideas on how this could be accomplished?

  • 3
    I'd probably start by implementing my own custom Drush command from a custom module, then in that command first do the CSS compilation and call drupal_flush_all_caches() in the end. – leymannx Aug 11 '20 at 18:11

Drush uses the underlying Drupal function, drupal_flush_all_caches(), which invocates of a couple hooks:

But these hooks are used for flushing/rebuilding cache data that any module is storing.

Given that you might not always need CSS compilation when clearing all caches (e.g. needing to refresh module metadata or do route rebuilding), you're better off coupling cache-clearing/CSS compilation at the shell command level (e.g. custom shell script, drush command, etc.) where it can be executed in a build process, and not at the application level.


Yes drush has hooks for running code before all commands or in this case just before cache:rebuild. See https://github.com/consolidation/annotated-command#hooks. See @pre-command hook for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.