MySQL: How to delete X day old entries

March 10, 2011 | In: mysql, web development

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