The Earth is round, a buttered toast will always fall butter-side down and doing clear cache is every Drupal developer's best practice, these are all globally-accepted truths! And speaking of the latter, when you discover that the familiar Drush clear cache technique is no longer universally unique you wonder: why the change?
Why go from clear-cache, to... actually rebuilding cache, starting with Drupal 8?
What's the catch?
This new way to clear Drupal cache must be stemming from a certain limitation that earlier versions of Drupal presented:
Partially completed cache-clearing operations threatening to grow into fatal errors.
And now, let's dig into more details on:
- clear Drupal cache: why & when
- the 4 methods for clearing your cache in Drupal
- Drush clear cache vs rebuilding cache: differences, the initiative behind this change, main benefits to expect
So, shall we proceed?
Clearing Your Drupal Cache: Why Bother? And When?
First of all, here's the “motivation” that drives Drupal to create a cache in the first place:
Each time a Drupal site has to render a certain web page, it is “forced” to perform specific database queries; and since all these queries have a negative impact on the overall page loading time, Drupal “decides” to store these web pages, once it will have rendered them, in a cache for later (streamlined) reference.
OK, now that we've settled this whole “cause and effect” process, let's see why and when you should clear cache on your Drupal site:
- when you're troubleshooting problems on your website; clear Drupal cache before you undertake any debugging, since this might just confirm to you that the “alerting issue” was nothing but a bad cache entry
- whenever you want Drupal to quickly record all the updates that you will have performed via the UI, all the changes you will have applied to your code
- when you're moving your website to a new host
- when you're installing a new theme or module on your Drupal site; just another scenario when Drush clear cache should be the very first step to take while you're troubleshooting
In a few words: clearing your cache might just be one of the most frequent actions you'll take while working (or simply maintaining) on a Drupal site.
And in many cases, the one that will “save the day”, without the need to apply other more complex techniques from your “arsenal”.
4 Different Methods to Clear Drupal's Cache
For there are several ways for you to clear your Drupal site's cache. Just go with the one that best suits your work style:
1. The Easy Way: Clear the Drupal Cache From the User Interface
By far the handiest (and some might say “the less-efficient”, too) method to clear Drupal cache is via the UI:
- just go to Administration>Configuration>Development>Performance
- and hit the “Clear all caches” button
It won't be long till Drupal displays the “Caches cleared” message for you! And that's it!
2. Drush Clear Cache (Drupal 7) or Drush Cache-Rebuild (Drupal 8)
And now, the second method in your “arsenal”: the clear Drupal cache command line one!
A two-way method, better said, which depends greatly on the version of Drupal on your website: 7 or 8?
In this respect, here's the “magic command” to use for clearing your Drupal 7's cache:
drush cache-clear all
drush cc all
Whereas in Drupal 8, this is the Drush command for tackling your cache:
or, alternatively, these 2 aliased commands:
drush rebuild or drush cr
And here I'm sure you can already tell which are the specific steps to take for handling your cache in Drupal 8 using Drush (still the most convenient way to do it):
- first of all, you open a Terminal window and CD in your Drupal 8 website's root (a step that you can overlook if it's Drush aliases that you're using):
- next, your run your “magic formula”, your Drush command (“drush cache-rebuild” or “drush cr”) and wait for it to complete its task before going back to your website
- and finally, you just reload the page you were on, in your web browser
3. Run the /core/rebuild.php file for Clearing Your Drupal 8 Site's Cache
Among all the improvements that Drupal 8 “lures” us in with (built-in WYSIWYG, a Twig templating system and so on), there's the /core/rebuild.php file standing out!
And “promising” us to streamline our frequent (and time-consuming) cache tackling tasks that we need to carry out during development:
The Drupal 8 site in question doesn't even have to be working and the whole process doesn't require Drupal Console or Drush either!
How about that?
The one and only requirement (for there still is one) is that your site's configuration supports it.
And how can you check whether your site's config accepts this functionality? Well, there 2 methods at your disposal:
- in case you're working locally, just ensure that $settings['rebuild_access'] = TRUE; in your settings.php (settings.local.php)
- or run this script in your command line: /core/scripts/rebuild_token_calculator.sh; then just use the results there as query parameters for /core/rebuild.php (https://goo.gl/qTrJ9d)
And voila! This “trick” will rebuild all cache without even requiring for the Drupal 8 site itself to be working during the whole process!
Which makes it the perfect “plan B”, whenever you don't have Drupal Console or Drush installed where you're working!
The only condition is that your websites' configuration supports this functionality!
4. In the Database: Truncate all Tables Starting With “cache_”
Spoiler alert: by “truncate” I do mean emptying, not removing!
The fourth method to clear Drupal cache involves clearing all the data from the cache-related tables in your database. Meaning all the tables starting with “cache_”.
For this, you just go over to your phpMyAdmin, select all the cache_* table and then click “Truncate” in the “with selected” drop-down menu placed at the bottom of the page:
As for the command line, feel free to scan through and then to tap into the valuable info that you'll find here: https://goo.gl/1b4otB here's another practical example:
Let's say it's Sequel Pro — an SQL GUI app — that you're using. For truncating those specific tables, connect to the server, track down your site's database, have those specific “cache_” tables highlighted and just choose “Truncate tables”, from the drop-down menu!
Also, in the above-mentioned “scenario” you could alternatively go to your PhPMyAdmin's SQL command field or MySQL CLI and run the above-mentioned command:
From Drush Clear Cache to Cache Rebuilding in Drupal 8: Why the Change?
Here's the challenge that the conventional Drush clear cache (or “drush cc all”) used to make us deal with:
Drupal's using caching intensively and therefore, it implicitely creates lots of inter-dependencies. Only partially flushing this heavy load of caches used to pose some major risks for any website.
This is where the “cache-rebuild” method stepped in, starting with Drupal 8!
It practically rebuilds (re-bootstraps) the whole Drupal site, after making sure that all cache is perfectly cleared. A “check and double check” technique, you may call it, which makes sure that:
- your site is up and running
- all cache gets flawlessly flushed!
Drupal 7's so very popular Drush cache command itself gets cleared and replaced with “cache-rebuild” in Drupal 8.
Which (the Drush cache-rebuild command specific to Drupal 8) carries out the following tasks:
- clearing the APC cache
- bootstrapping Drupal
- calling drupal_rebuild()
- removing the Drush cache
Summing it up now, the essential info to remember is that:
- “clear cache” should be on top of your “best practices” list as a Drupal developer
- you have not just 1, but 4 methods to choose from, depending on your work style and context: via the UI, clear cache using Drush, by truncating your “cache_” database tables, by running the /core/rebuild.php file
- Drupal 8's cache-rebuild is a step forward from the conventional cache-clear practice; it adds a new “re-bootstrapping” operation to the “cache clearing” process!