Skip to content Skip to sidebar Skip to footer

How To Delete Mysql Data From Table At Midnight?

I have a mysql table and I want it to be 'emptied' every night at midnight. I have searched for an answer on the web and came across nothing that seemed to help me. I had this idea

Solution 1:

Implementing your own event scheduler, especially as a web page using JavaScript is a bad idea. Use for that either

  • a cron job to run DELETE statement through the mysql command line interface
    /path/to/mysql -u<user> -p"<password>" <db_name> -e "delete from messages"
CREATE EVENT delete_messages_at_midnight 
     ON SCHEDULE EVERY1DAY STARTS CURDATE() +INTERVAL1DAY
     DO DELETEFROM messages;

If you go with MySQL event approach:

  • use SHOW PROCESSLIST to check if the event scheduler is enabled. If it's ON you should see a process "Daemon" by user "event_scheduler".
  • use SET GLOBAL event_scheduler = ON;to enable the scheduler if it's currently not enabled.
  • More on configuring event scheduler read here

Post a Comment for "How To Delete Mysql Data From Table At Midnight?"