I want to create script to cleaning magento logs. How can I do this?

2 Answers 2


For visitor logs in the database, Magento 1.9.x already has a cron job that can be run for this.

Go to here to find the automatic log cleaning schedule.

System > Configuration > Advanced > System > Log Cleaning

For clearing out the exception.log, system.log, and any other residents of var/log/*.log I would recommend using something like logrotate, which is a pretty standard log rotation tool for Linux systems that you can use to compress logs in daily/weekly/monthly rotations. See here for a nice Digital Ocean tutorial on setting up log rotation

Here's a template I generally use for setting up logrotate on M1.9.x

File: /etc/logrotate.d/magento

/path/to/magento/var/log/*.log {
    create 664 file_owner file_group
    rotate 31
        kill -USR1 `cat /var/run/nginx.pid`

Note that the part of that script below is only for nginx based servers

    kill -USR1 `cat /var/run/nginx.pid`
  • this is not working. Commented Aug 1, 2018 at 11:33
  • Which of these is not working? The automated visitor logs for cleaning tables or using logrotate for system logging? Commented Aug 1, 2018 at 12:07

In simple way,you can do it.

Create a php script at Magento application root dir where cron.php file is located.

the using mysql query truncate data from database table

add This php script at cronjob as per as your requirement

require_once 'app/Mage.php';
Mage::app("admin"); // run application as admin
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$query1 = "truncate table dataflow_batch_export";
$query2 = "truncate table dataflow_batch_import";
$query3 = "truncate table log_customer";
$query = "truncate table log_quote";
$query4 = "truncate table log_summary";
$query5 = "truncate table log_summary_type";
$query6 = "truncate table log_url";
$query7 = "truncate table log_url_info";
$query8 = "truncate table log_visitor";
$query9 = "truncate table log_visitor_info";
$query10 = "truncate table log_visitor_online";
$query11 = "truncate table report_viewed_product_index";
$query12 = "truncate table report_compared_product_index";
$query13 = "truncate table report_event";


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.