Knowledge Base

Magento Log and Cache Maintenance Script

Did you find this article helpful?
Copy Link

* Your feedback is too short
Share this article
  1. Create a file called cleanup.php and add the following code to it (where your specific info is subbed into the section below 'Magento Maintenance Script')

     * Magento Maintenance Script
     * @version    3.0.1
     * @author     Web Hosting Company <[email protected]>
     * @copyright  Copyright (c) 2006-2013 Web Hosting Company
     * @link       http://www.thatsite.com  Web Hosting Company
    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.'`');
        } else {
            exit('Unable to load local.xml file');
    function clean_var_directory() {
        $dirs = array(
        foreach($dirs as $dir) {
            exec('rm -rf '.$dir);
  2. Save the file to the directory where Magento resides

  3. Log into cPanel and click the Cron Manager link

  4. In the Add Cron Job section, select Once a day from the Common Settings dropdown list

  5. In the Command field, enter the following line of code, making sure to replace the domain name with your own:

    curl -s -o /dev/null http://www.domain.com/cleanup.php?clean=log
  6. Once you have this set at your preferred interval, click the Add Cron Job button

  7. Repeat the same steps as you did before, but change the Common Settings dropdown selection to 1st and 15th, and add the following line of code to the Command field:

    curl -s -o /dev/null http://www.domain.com/cleanup.php?clean=var
  8. Now click the Add Cron Job button

  9. While you're on this page, it's a good idea to set the Email Address to something other than your username, otherwise your mail/new directory will fill up very quickly every time a cron job runs (assuming it produces output); You can leave it blank or use an actual email address

Did you find this article helpful?
Copy Link

* Your feedback is too short