How can I Automatically Clear Cache for Drupal using Gulp?

February 5, 2018

Drupal is a CMS that relies pretty heavily on caching, and for whatever reason, there isn’t a great way to disable this for development which makes editing your templates a little more frustrating. Saving your file and clearing your cache for every little change gets annoying quickly, especially when you forget about and can’t seem to figure out why nothing is showing up, we’ve all been there.

To improve your Drupal development workflow, I’d recommend using Gulp. Gulp is a toolkit for automating painful or time-consuming tasks in your development workflow, so you can do more with less work. One of the tasks you can set up with Gulp is cache clearing. For the purpose of this article, I’m going to assume you’ve worked with Gulp in the past so we won’t dig into the initial setup of Gulp in a theme, but focus more on the cache clearing task.

The first thing you’ll need is to import the dependency for “child process.” Next, you’ll want to set up an array variable containing all the paths to files you want auto-cache clearing setup for. This will basically be any Drupal template file. I would start with the main template directories you know you need it set up for and then add the additional paths along the way. Once we have our variables set, we then build our “clearcache” task using the “child_process” dependency that is shown below. Once we have all the pieces setup, we combine them in a watch task and let the magic happen! Using this task in your workflow will save you so much time, and even more importantly, save you the headache of clearing your cache every 2 minutes.

If you do run into some issues having your task running, make sure your Drush is setup and working in your command line as these Gulp task relies on Drush to complete its work.

var cp = require('child_process');
var drupal_files = "paths/to/your/drupal/files/you/want/auto-cache-clearing";

// Clearcache Task
gulp.task('clearcache', function(done) {
  return cp.spawn('drush', ['cache-rebuild'], {stdio: 'inherit'})
  .on('close', done);
});, ['clearcache']);

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive