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.
Asked
Active
Viewed 399 times
1
-
Set up a job to run every day, clear out entries that are older than 30 days. – Qirel Apr 04 '19 at 11:29
-
sounds like a job for a cronjob! – Jeff Apr 04 '19 at 11:29
2 Answers
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

user9343019
- 54
- 5
-
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