Magento Log & Cache Maintenance Script

Create a file called cleaner.php and add the following code to it:


switch($_GET['clean']) {

case 'log':
 case 'var':

function clean_log_tables() {
 $xml = simplexml_load_file('./app/etc/local.xml', NULL, LIBXML_NOCDATA);

if(is_object($xml)) {
 $db['host'] = $xml->global->resources->default_setup->connection->host;
 $db['name'] = $xml->global->resources->default_setup->connection->dbname;
 $db['user'] = $xml->global->resources->default_setup->connection->username;
 $db['pass'] = $xml->global->resources->default_setup->connection->password;
 $db['pref'] = $xml->global->resources->db->table_prefix;

$tables = array(

mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error());
 mysql_select_db($db['name']) or die(mysql_error());

foreach($tables as $table) {
 @mysql_query('TRUNCATE `'.$db['pref'].$table.'`');
 echo "All log files have been cleaned.";
 } else {
 exit('Unable to load local.xml file');

function clean_var_directory() {
 $dirs = array(

foreach($dirs as $dir) {
 exec('rm -rf '.$dir);
 echo "All var files have been cleaned.";


Save this file to the root of your Magento directory.


You can run this file either by hitting the URL directly through your web browser,like:


OR, you can set a cron job for this. Open cPanel, go to Cron Jobs->Add New Cron Job. Set the cron job to run once a day and add following command:

php -f /home/<yourhostname>/cleaner.php?clean=log

Repeat the same steps, but change the Common Settings dropdown selection to 1st and 15th (0 0 1,15 * *),and add the following command:

php -f /home/<yourhostname>/cleaner.php?clean=var

Leave a Reply

Your email address will not be published. Required fields are marked *