MySQL: How to delete X day old entries

In some cases we need to remove old entries from the database or remove entries before specific days. So mysql provides the syntax to delete records:

 DELETE FROM $table_name WHERE $date_field < date_sub(CURDATE(), $numberOfDays day)

If above query does not work for you then its version issue. So you can do same thing like this:

  DELETE FROM $table_name WHERE DATEDIFF(CURDATE(), $date_field) >= $numberOfDays

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*