3

How can I go about clearing all of Magento's caches and logs by running a PHP script?

1
10

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

    <?php
switch($_GET['clean']) {
    case 'log':
        clean_log_tables();
    break;
    case 'var':
        clean_var_directory();
    break;
}

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(
            'adminnotification_inbox',
            'aw_core_logger',
            'dataflow_batch_export',
            'dataflow_batch_import',
            'log_customer',
            'log_quote',
            'log_summary',
            'log_summary_type',
            'log_url',
            'log_url_info',
            'log_visitor',
            'log_visitor_info',
            'log_visitor_online',
            'index_event',
            'report_event',
            'report_viewed_product_index',
            'report_compared_product_index',
            'catalog_compare_item',
            'catalogindex_aggregation',
            'catalogindex_aggregation_tag',
            'catalogindex_aggregation_to_tag'
        );

        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(
        'downloader/.cache/',
        'downloader/pearlib/cache/*',
        'downloader/pearlib/download/*',
        'var/cache/',
        'var/locks/',
        'var/log/',
        'var/report/',
        'var/session/',
        'var/tmp/'
    );

    foreach($dirs as $dir) {
        exec('rm -rf '.$dir);
    }
}

Save the file to the magento root directory.

Run the following url For Log clearing

http://www.domain.com/cleanup.php?clean=log

Run the following url for Cache clearing

http://www.domain.com/cleanup.php?clean=var

1
  • 3
    You shouldn't truncate the table log_summary_type
    – Marius
    Nov 23 '13 at 9:48
6

You're best of doing this from the command line using a SSH terminal or a cronjob

# rm -Rf /home/domain.com/www/var/cache/*
# rm -Rf /home/domain.com/www/var/log/*

This will be more reliable than using PHP

5
  • why should log be cleared unless its necessary. Aren't they useful to sort out error ?
    – Dexter
    Nov 22 '13 at 8:31
  • they are but OP asked for a way to delete the log files so that's what I gave. :)
    – Sander Mangel
    Nov 22 '13 at 8:33
  • ohh sorry.. i didn't noticed the log files question :)
    – Dexter
    Nov 22 '13 at 8:33
  • No problem. But you're right. It would be better to make a bash script that packs the log files every day and renames them by date so you have backups. Optionally delete those when they're older than X days
    – Sander Mangel
    Nov 22 '13 at 8:41
  • its a great idea... i have created a script for downloading and deleting log file monthly.
    – Dexter
    Nov 22 '13 at 9:09
2

You can call the following from php.

Mage::app()->cleanCache();
2
  • This is pretty cool technique. Do i run this in terminal after starting php -a???? or do i create a file an add this into the file and then just visit the particular page?? @David Manners
    – Supplement
    Feb 3 '17 at 1:37
  • you could create your own magento shell script that does this. Or you could consider using n98-magerun Feb 3 '17 at 8:01
2
  • For clean log, shell script already exist in path-to-magento/shell directory:

    php log.php 
    Usage:  php -f log.php -- [options]
            php -f log.php -- clean --days 1
    
      clean             Clean Logs
      --days      Save log, days. (Minimum 1 day, if defined - ignoring system value)
      status            Display statistics per log tables
      help              This help
  • For clean cache, following methods will help you to make a shell script:

    // Clean js and css
    Mage::getModel('core/design_package')->cleanMergedJsCss();
    Mage::dispatchEvent('clean_media_cache_after');
    
    // product image
    Mage::getModel('catalog/product_image')->clearCache();
    Mage::dispatchEvent('clean_catalog_images_cache_after');
    
    // Get cache types
    Mage::app()->getCacheInstance()->getTypes()
    
    // Clean specific tags
    Mage::app()->cleanCache($tags);
    
    // dispatch event if you flush all cache tags
    Mage::app()->cleanCache($alltags);
    Mage::dispatchEvent('adminhtml_cache_flush_system');
    
1
  • I prefer an answer along these lines because it doesn't assume that the file cache is the one being used. There's also database, memcached, etc. which won't be cleared by emptying the var/cache folders. Apr 14 '14 at 13:42
1

Goto system->Cache Management->Flush Magento Cache from admin panel OR clear var/cache directory.

0

I would suggest, first you try to find the tables with most rows & size by using SQL:

SELECT
  TABLE_NAME AS "Table",
  TABLE_ROWS AS "Rows #",
  ROUND(
    (DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024,
    2
  ) AS "Size (MB)"
FROM
  information_schema.TABLES
WHERE information_schema.TABLES.TABLE_SCHEMA = 'database-name'
  AND (
    TABLE_NAME LIKE 'log_%'
    OR TABLE_NAME LIKE 'report_%'
    OR TABLE_NAME LIKE 'dataflow_%'
    OR TABLE_NAME = 'catalog_compare_item'
  )
ORDER BY TABLE_ROWS DESC

And you can run the truncate operation on the log tables.

TRUNCATE dataflow_batch_export;
TRUNCATE dataflow_batch_import;
TRUNCATE log_customer;
TRUNCATE log_quote;
TRUNCATE log_summary;
TRUNCATE log_summary_type;
TRUNCATE log_url;
TRUNCATE log_url_info;
TRUNCATE log_visitor;
TRUNCATE log_visitor_info;
TRUNCATE log_visitor_online;
TRUNCATE report_viewed_product_index;
TRUNCATE report_compared_product_index;
TRUNCATE report_event;
TRUNCATE index_event;
TRUNCATE catalog_compare_item;

For more, please visit: https://blog.magepsycho.com/how-to-find-the-size-rows-of-magento-database-tables/

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.