Is there any way to clean logs and tables automatically in Magento 2 server? The logs are just flooding unless I delete them manually. Frequently it leads to server downtime as well.
This github issue mentions that no such commands are available in Magento 2 unlike Magento 1 where log cleaning was available via admin as well as commands. But that is a pretty old one, hence the ask. Couldn't find many helpful posts either.
Any suggestions will be helpful, thanks in advance!
I was going to say just clean it once a week or something because some people actually read logs for various purposes.
if you want them off :
bin/magento setup:config:set --enable-debug-logging=false
By default, Magento writes to the debug log install_directory/var/log/debug.log) when it is in default or develop mode, but not when it is in production mode.
is better to have them because is were people go first and check errors.
just set a cron to delete once a week. if the log is reaching gigabites levels once a week then something is wrong already.
Try running below SQL query in your database
SET foreign_key_checks = 0; TRUNCATE customer_log; TRUNCATE customer_visitor; TRUNCATE report_compared_product_index; TRUNCATE report_event; TRUNCATE report_event_types; TRUNCATE report_viewed_product_aggregated_daily; TRUNCATE report_viewed_product_aggregated_monthly; TRUNCATE report_viewed_product_aggregated_yearly; TRUNCATE report_viewed_product_index; TRUNCATE product_alert_stock; TRUNCATE search_query; TRUNCATE catalogsearch_fulltext_scope1; TRUNCATE sales_bestsellers_aggregated_yearly; TRUNCATE sales_bestsellers_aggregated_monthly; TRUNCATE sales_bestsellers_aggregated_daily; SET foreign_key_checks = 1;
It will clear all the cache, you can make this trigger and configure it within your phpmyadmin to run it automatically at specified time.
there is an error when running the sql:
Error consulta SQL: TRUNCATE report_event_types MySQL ha dicho: Documentación #1701 - Cannot truncate a table referenced in a foreign key constraint (`demomagento2_bd`.`report_event`, CONSTRAINT `REPORT_EVENT_EVENT_TYPE_ID_REPORT_EVENT_TYPES_EVENT_TYPE_ID` FOREIGN KEY (`event_type_id`) REFERENCES `demomagento2_bd`.`report_event_types` (`eve)
I think there isn't an option available to clear the logs for the Magento app via the admin panel. If you are facing a problem with logs management and want to clear, you can do it by configuring cron job management for clearing logs automatically.
I hope, you find your answer. If you still have any queries, feel free to ping me.
Cleaning Logs via Magento Admin Panel
This method is easier for non technical store owners who don’t want’ to mess directly with the Magento store’s database. To activate log cleaning option in Magento just do the following:
Log on to your Magento Admin Panel. Go to System => Configuration. On the left under Advanced click on System (Advanced = > System). Under system you will see “Log Cleaning” option. Fill the desired “Log Cleaning” option values and click Save.
Cleaning Logs via phpMyAdmin
If you are comfortable with mysql and queries then this method is more efficient and quicker than default Magento Log Cleaning tool. This method also allows your to clean whatever you like, you can even clean tables which aren’t included in default Magento’s Log Cleaning tool.
Open the database in phpMyAdmin In the right frame, click on the boxes for the following tables: dataflow_batch_export
Look to the bottom of the page, then click the drop down box that says “with selected” and click empty. Click Yes on confirmation screen, and this will truncate all the selected tables.
or you can use script to run
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;
Keep in mind that we are here to empty (Truncate) selected tables are not drop them. Be very careful when you do this.