Drupal - How to clear cache externally?
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
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');
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