Create a file called cleanup.php and add the following code to it:
<?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',
'report_event'
);
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.'`') or die(mysql_error());
}
}
function clean_var_directory() {
$dirs = array(
'downloader/pearlib/cache/*',
'downloader/pearlib/download/*',
'var/cache/',
'var/log/',
'var/report/',
'var/session/',
'var/tmp/'
);
foreach($dirs as $v => $k) {
exec('rm -rf '.$k);
}
}
?>
Save the file to the directory where Magento resides.
Next, open up cPanel and click on the Cron Jobs icon.
Click on the Standard button.
For the Command to run, 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
I would recommend leaving the time settings as is, so your log tables will be cleared out daily at 3 AM.
Once you have this set at your preferred interval, click the Save Crontab button.
If you want to have your var directory cleared out automatically, add the following line of code to a new entry:
curl -s -o /dev/null http://www.domain.com/cleanup.php?clean=var
It would be best to clean this out every 2-4 weeks, so set the Day(s) column to 14 or 30. Click the Save Crontab button once you're done.
This script may not work in all hosting environments, and I take no responsibility if you fubar your site up!