Posts Tagged ‘log cleaning’

Hello,

Magento maintains several tables for logging. These tables log things such as customer accesses and which products have been compared. Magento has a mechanism for cleaning these logs regularly, but unfortunately this feature is disabled by default and most customers do not turn it on.

The following tables are managed by Magento’s Log Cleaning function:


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`;

You can use below script file and use it by scheduling cronjob. Filename will be cleanup.php


<?php
$xml = simplexml_load_file('./app/etc/local.xml', NULL, LIBXML_NOCDATA);

$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;

if($_GET['clean'] == 'log') clean_log_tables();
if($_GET['clean'] == 'var') clean_var_directory();

function clean_log_tables() {
	global $db;

	$tables = array(
		'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_compared_product_index',
		'report_viewed_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 $v => $k) {
		@mysql_query('TRUNCATE `'.$db['pref'].$k.'`');
	}
}

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 $v => $k) {
		exec('rm -rf '.$k);
	}
}
?>

Save the file to the root directory of Magento code.

Next, open cPanel and click on the Cron Manager link.

In the Add Cron Job section, select Once a day from the Common Settings dropdown list. In the Command field, enter the following line of code, making sure to replace the yourdomain name with your own:


curl -s -o /dev/null http://www.yourdomain.com/cleanup.php?clean=log

Once you have this set at your preferred interval, click the Add Cron Job button.

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.yourdomain.com/cleanup.php?clean=var

Now click the Add Cron Job button.

I hope above content is useful to you. Thanks for reading my blog.

Thanks,
Bijal Bhavsar 🙂