Drupal cache clean up - cache_clear_all()

Drupal cache clean up - cache_clear_all()



We started discussing how to clean up Drupal cache tables on the How to delete or clear Drupal cache tables? page. That page talks about using the Drupal Devel module to clean up Drupal's cache.

The Devel module is the preferred way of clearing cached data because it is internal to Drupal and has been tested by many people. However, some administrators and developers may need to have more control of what gets deleted and how. If you want to get into the custom Drupal code, you can use the cache_clear_all() function which is defined in the cache.inc file.

Delete Drupal cache - option 2: cache_clear_all() function

The cache_clear_all() function is a Drupal function that can be used to delete cache tables.

What does the cache_clear_all() function do?

The cache_clear_all() function is internal resource to Drupal and can be used to delete data in the Drupal cache tables selectively. We will focus on just cleaning the Drupal cache completely.

How do I set up the cache_clear_all functionality?

There are many ways how you can set up Drupal to access the functionality of deleting Drupal cache. We prefer setting this up via a combination of a page and a block.

Step 1: Set up a page...

First, set up a page using the page content type. When editing this page, do not include any content on it and give it some misleading Title name, so that if anyone comes to this page by mistake, they do not know what this page really is.

URL: In case you are using clean URLs, leave the box for clean URLs empty. You want this page to be hidden behind the anonymous default number.

Menu: If you like, include this page in your Administer menu. Name the link Clear cache, for example.

Robots: You do not want this page to be indexed by robots, so include noinxex, nofollow in case you have your meta tags editing enabled.

Publish: Publish this page but do not promote it to the first page.

Step 2: Set up block...

Again, you would not want your Empty cache functionality to be accessible by anyone else but your or your admin. Depending on how your website is setup, you have many different options of how to restrict who can access your Empty cache functionality. One of the nice ways is to create a custom block that will be accessible by the admin role only.

Go to your Administer -> Blocks screen and add a new block. In this block, select PHP input and include a php code something like the following:

    <?php
    cache_clear_all('*', 'cache', TRUE);
    cache_clear_all('*', 'content', TRUE);
    cache_clear_all('*', 'cache_page', TRUE);
    cache_clear_all('*', 'cache_menu', TRUE);
    cache_clear_all('*', 'cache_filter', TRUE);
    cache_clear_all('*', 'cache_view', TRUE);
    ?>

Modify this code for all the tables you want to include in the clean up functionality and omit those tables from the code that you do not have in your database.

After saving this block, go to the configuration screen associated with this block and add permissions for the admin role. Also take the URL of the page that you created in step 1 and mention it at the bottom of the screen in the box with pages where this block is supposed to show up (Page specific visibility settings -> Show on only the listed pages).

At the end of this exercise, you should have a link called Clear cache in your Administer menu. Clicking this link takes you to the page where if you are logged in as an administrator, a script will get executed which cleans up your cache tables. If you come to this page as an anonymous user, no script will run, and the page will be just blank.

Drupal cache, throttle, and expire

A few important points. If you delete your cache tables using Devel or the clear_cache_all function and go to the database, you will see that your cache tables are populated with some data again (just a little of data, but still there will be something..). This can happen for two reasons.

a) Drupal instantly caches any blocks that you have throttled if you use throttle. So, even if you delete your Drupal cache, some of it comes right back.

b) The Devel module and the cache_clear_all function delete only data that does not have zero in the expiry column on the cache tables. So, if you really really want to clean up your web, you probably need to dump the remaining lines with expiry=0 manually.

Delete Drupal cache - option 3: SQL query delete from

For those who need to get into more details of deleting the Drupal cached data, there is always the hard-core way of deleting the data directly.

SQL delete from

Another way of deleting Drupal cached data is to go into the database directly and delete data using a SQL query.

Although a SQL query such as delete from cache; technically clears your cache table as well, it is advised to be careful. This deletes the data, but depending on the database, it may not reset table index. Not resetting table index after deleting data can create problems.

.

Discuss this article or this topic in our discussion forum:
(The table bellow shows a list of 8 most recent topics posted in our discussion forum. Visit our discussion forum to see more. It is possible the links below are not related to this page, but you can be certain you will find related posts in the discussion forum. You can post one yourself too.)
Email this article to a friend:
TO: 
FROM: 
2 + 6 - 3 = 
.
How can I link to this web page?

It is easy, just include the code provided below into your HTML code.

<a href="http://www.maxi-pedia.com/Drupal+cache+clean+up+cache_clear_all+function" title="www.Maxi-Pedia.com: Drupal cache clean up - cache_clear_all()" target="_blank">Drupal cache clean up - cache_clear_all()</a>
.