Last updated 11 July 2017. Created on 22 October 2015.
Edited by fabian.marz, joelpittet, Nicolas Bouteille, finne. Log in to edit this page.

When you are developing a module or theme you have to clear Drupal's cache (render cache, dynamic page cache, Twig cache) very often to see the changes being applied. You can disable caching for development.

So if your terminal looks like this:

$ drush cr
Cache rebuild complete.                      [ok]
$ drush cr
Cache rebuild complete.                      [ok]
$ drush cr
Cache rebuild complete.                      [ok]

...

then try the following:

Enable local development settings

1. Copy and rename the sites/example.settings.local.php to sites/default/settings.local.php:

$ cp sites/example.settings.local.php sites/default/settings.local.php

2. Open settings.php file in sites/default and uncomment these lines:

if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
  include $app_root . '/' . $site_path . '/settings.local.php';
}

This will include the local settings file as part of Drupal's settings file.

3. Open settings.local.php and uncomment (or add) this line to enable the null cache service:

$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';

By default development.services.yml contains the settings to disable Drupal caching:

services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

NOTE: Do not create development.services.yml, it exists under /sites

4. In settings.local.php change the following to be TRUE if you want to work with enabled css- and js-aggregation:

$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;

5. Uncomment these lines in settings.local.php to disable the render cache and disable dynamic page cache:

$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

If you do not want to install test modules and themes, set the following to FALSE:

$settings['extension_discovery_scan_tests'] = FALSE;

6. Open development.services.yml in the sites folder and add the following block to disable the twig cache:

parameters:
  twig.config:
    debug: true
    auto_reload: true
    cache: false

NOTE: If the parameter block is already present in the yml file, append the twig.config block to it.

7. Afterwards you have to rebuild the Drupal cache otherwise your website will encounter an unexpected error on page reload. This can be done by with drush:

drush cr

or by visiting the following URL from your Drupal 8 website:

http://yoursite/core/rebuild.php

Now you should be able to develop in Drupal 8 without manual cache rebuilds on a regular basis.

Your final development.services.yml should look as follows (mind the indentation):

# Local development services.
#
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
parameters:
  http.response.debug_cacheability_headers: true
  twig.config:
    debug: true
    auto_reload: true
    cache: false
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

Use Drupal Console development mode

You can use Drupal Console to switch site into development mode:

Use site:mode command like this:

drupal site:mode dev

The command will override site's services.yml file and console output will show you which parameters has been changed:

$ drupal site:mode dev
 Configuration name: system.performance
 ------------------------- ---------------- ----------------
  Configuration key         Original Value   Override Value
 ------------------------- ---------------- ----------------
  cache.page.use_internal                    false
  css.preprocess            true             false
  css.gzip                  true             false
  js.preprocess             true             false
  js.gzip                   true             false
  response.gzip                              false
 ------------------------- ---------------- ----------------

 Configuration name: views.settings
 -------------------------------- ---------------- ----------------
  Configuration key                Original Value   Override Value
 -------------------------------- ---------------- ----------------
  ui.show.sql_query.enabled        false            true
  ui.show.performance_statistics   false            true
 -------------------------------- ---------------- ----------------

 Configuration name: system.logging
 ------------------- ---------------- ----------------
  Configuration key   Original Value   Override Value
 ------------------- ---------------- ----------------
  error_level         hide             all
 ------------------- ---------------- ----------------


 Services files C:\xampp5628\htdocs\d825/sites/default/services.yml was
 overwritten


 New services settings
 ------------- ------------- -------
  Service       Parameter     Value
 ------------- ------------- -------
  twig.config   auto_reload   true
  twig.config   cache         true
  twig.config   debug         true
 ------------- ------------- -------

 cache:rebuild

 Rebuilding cache(s), wait a moment please.


 [OK] Done clearing cache(s).

$

Notes:

  • No activation of local.settings.php in settings.php
  • Use $drupal site:mod prod to revert

Don't add comments to this page. Edit it instead

Find cache bins

To find all available (but not necessarily enabled) cache bins:

find . -type f -name *.services.yml | \
xargs sed -E -n  's/.*cache\.(.*):.*/\1/p' | \
grep -v "backend\|html.twig" | \
sort -u | \
awk -vORS=\',\' '{ print $1 }' | \
sed "s/,'$//" | sed "s/^/'/"

Note that in OSX you might need to install and run 'gawk' instead of 'awk' in order for the above command to work. You can use brew to install gawk:

brew install gawk

Then you could copy/paste it in your settings.php file and disable caching like this:

$cache_bins = array('bootstrap','config','data','default','discovery','dynamic_page_cache','entity','menu','migrate','render','rest','static','toolbar');
foreach ($cache_bins as $bin) {
  $settings['cache']['bins'][$bin] = 'cache.backend.null';
}

Disable cached response headers

If HTTP headers include cached Response Headers: Expires and Request Headers: Cache-Control then it is probably good to disable them during development:

On MAMP 4+ uncheck Apache settings cache_module. You might have to disable disk_cache_module and mem_cache_module as well if you are having problems restarting your apache. Close and re-open MAMP 4+.

Disable caching in your browser

Although Drupal is not caching, your browser does. That can be confusing when you have disabled all caching in the settings files and your CSS changes are not visible after a webpage reload. For example, in Google Chrome you can disable caching while the dev tools are opened:

Disable caching in Google Chrome

Disable caching in Google Chrome

Alternative: develop with caching enabled

Disabling caching has some drawbacks. The whole site responds slower, and rebuilding all caches takes a while too. Doing this a lot, and all those seconds waiting for a response add up. For an approach to developing with the cache enabled, see https://www.dx-experts.nl/drupal-8-development-caching.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.