What is the best way to delete old rows from MySQL on a rolling basis?

Try creating Event that will run on database automatically after the time interval you want.

Here is an Example:
If you want to delete entries that are more than 30 days old from some table ‘tableName’, having column entry ‘datetime’. Then following query runs every day which will do required clean-up action.

CREATE EVENT AutoDeleteOldNotifications
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY 
ON COMPLETION PRESERVE
DO 
DELETE LOW_PRIORITY FROM databaseName.tableName WHERE datetime < DATE_SUB(NOW(), INTERVAL 30 DAY)

We need to add ON COMPLETION PRESERVE to keep the event after each run. You can find more info here: http://www.mysqltutorial.org/mysql-triggers/working-mysql-scheduled-event/

Leave a Comment