7

I'd like some help how to clear the cache using an external script. I have a script that adds nodes each night, and I'd also like it to clear the views cache so - without having to make out by myself the MySQL query.

7

If you just want to clear one or two particular cache bins you can use cache_clear_all() (assuming your script has Drupal bootstrapped):

cache_clear_all(NULL, 'cache_views');
cache_clear_all(NULL, 'cache_views_data');
1
  • Since it is a non-Drupal script, I'll probably do it like that, and include() the necessary files. Thanks! – Daniel Holm Sep 24 '12 at 21:34
11

If you are running a script, you'll likely benefit from using Drush. With the Drush php-eval and php-script commands, you can easily call a snippet of php code (such as the examples shown in other answers to this question) after bootstrapping Drupal. Without Drush, you would have to set up a web service and use wget or curl to run your code, or try to call the Drupal bootstrap code yourself. Better yet, Drush even comes with a built-in command to clear the cache, so you don't need to worry about writing any php code at all, if that's all you want to do from your script. Just use:

cd /path/to/drupal/sites/default && drush cache-clear all

You may also wish to learn about site aliases; if you define an alias called @site, then you can instead use:

drush @site cache-clear all
3
  • Drush seems like the clear solution here, though OP wants to clear Views cache. Try drush cc views in your script as a user with the correct access. – Citricguy Sep 17 '12 at 23:06
  • Yes, you are correct about drush cc views; however, Drush commands by convention usually call API functions directly, without access checks; it is not usually necessary to specify a user. – greg_1_anderson Sep 18 '12 at 1:07
  • Unfortunately, I do not have that kind of access on this server. Otherwise I really much enjoy Drush! – Daniel Holm Sep 24 '12 at 21:36
6

I suggest register menu and externally call it ,in call back of it put your code(clear cache )

function yourmodule_menu() {
    $items = array();
    $items['customclearcache'] = array(
        'title' => 'clear cache',
        'description' => 'clear cache',
        'page callback' => 'yourmodule_clear_cache',
        'access callback' => TRUE , // or any access you need
    );
    return $items;
}


 function yourmodule_clear_cache(){
   cache_clear_all(NULL, 'cache_views');
   cache_clear_all(NULL, 'cache_views_data');
   drupal_set_message(t('cache clearing completed'));
   drupal_goto("node"); // or any page you want
}

and you can clear cache with calling this url : yoursite.com/customclearcache.

Also if you installed drush clear cache with it drush cc all

   //first go to your installed site path
  $cd /path/to/drupal 
   $drush cc all
6
  • 5
    Make sure to check access, repeatedly calling that url could otherwise easily bring your site down – Berdir Sep 17 '12 at 22:16
  • @Berdir , yes, I am agree with you, this just a way can get idea and develop it. – Yuseferi Sep 18 '12 at 4:13
  • The script is outside of actual Drupal code. This would be perfect for my module, but not in this precise case. But thanks! – Daniel Holm Sep 24 '12 at 21:37
  • A callback like this should check some secret token and ideally an anti-csrf token to avoid abuse. – greggles Dec 12 '15 at 21:30
  • @greggles yes I agree with you , or specified access callback ;) – Yuseferi Dec 13 '15 at 5:24
0

Best way to clear the cache externally is by using Drush commands.

You could use drush cc in the terminal and it will ask you what exactly you want to clear. In example it will ask for:

Enter a number to choose which cache to clear.

 [0]   :  Cancel         
 [1]   :  all            
 [2]   :  drush          
 [3]   :  theme-registry 
 [4]   :  menu           
 [5]   :  css-js         
 [6]   :  block          
 [7]   :  module-list    
 [8]   :  theme-list     
 [9]   :  registry       
 [10]  :  views   

Select you option and you could clear the cache externally.

For more details about the drush and commands of it you could visit drush GitHub page.

0

If you want to clear the cache externally, the easiest way is to use drush, for example:

drush -y cc all

which would clear all Drupal caches (but not all caches). In Drupal 8: drush cr.

Other way is to write a simple script which would truncate/delete all tables which starts with cache_, for example using the following one-liner:

echo "SHOW TABLES LIKE 'cache%'" | drush sqlc --extra=--skip-column-names | xargs -L1 -I% echo "DELETE FROM %;" | drush sqlc --extra=-v 
-2

TRUNCATE the cache_views table in the database.

2
  • Sorry. Didn't read carefully enough. – Gaelan Sep 18 '12 at 23:20
  • Its alright. But yeah, I didn't want to it like that, but rather using a "safe" Drupal function. – Daniel Holm Sep 24 '12 at 21:35

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.