Wednesday, September 21, 2011

Delete data in MySQL Older than x days

Deleting data in MySQL that older than [x] specified time. MySQL has INTERVAL to specified MINUTE, HOUR, DAY and YEAR.

If data type is MySQL Timestamp, Datetime:

DELETE
FROM my_table 
WHERE time_created < (NOW() - INTERVAL 10 DAY);

If data type is UNIX Timestamp:

DELETE
FROM my_table 
WHERE time_created < (UNIX_TIMESTAMP() - 600);

Note that UNIX Timestamp in second, statement above meaning 600 seconds => 10 minutes

No comments:

Post a Comment