1

I have a table named user_logs. I want to remove the logs every 30 days. Is there any possible way to do it automatically. I am using php and mysql in my project.

2 Answers2

2

You could setup an Event to delete record older than 30 days, like :

CREATE EVENT IF NOT EXISTS `Clean_Older_Than_1_month_logs`
ON SCHEDULE
EVERY 1 DAY_HOUR
COMMENT 'Clean up user_logs older than 1 month.'
DO
    DELETE FROM user_logs
    WHERE user_logs_date < DATE_SUB(NOW(), INTERVAL 1 MONTH)
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
0

Here, You should use 'setInterval()' in Js for ajax call after 30-days.

index.html page

$(document).ready(function(){
    setInterval(function(){ 
        alert("Event Fire");
        $.ajax({
            url: "30_day_event.php",
            success: function(result){
                alert('database operation done');
            }
        });
    }, 1000*60*60*24*30);
});

30_day_event.php page

//your your logic to delete data from user_logs table
//your callback code
  • People would have to keep the webpage open for 30 days though. Not very user friendly – Geert Apr 04 '19 at 14:17
  • 'setInterval(function(){ alert("Event Fire"); $.ajax({ url: "30_day_event.php", success: function(result){ alert('database operation done'); } }); }, 1000*60*60*24*30);' if you convert this code snippet in node.js it will work with just severer running. – user9343019 Apr 05 '19 at 04:19