Single post

Deleting rows older than 30 days in MySQL

If you want to delete rows older than xx dates using MySQL, here is a simple way of doing so:

DELETE FROM mytable WHERE date < DATE_SUB(NOW(), INTERVAL 30 DAY);

You could also use INTERVAL 1 MONTH.

June 2nd, 2008 at 8:59 pm

Hey Thanks! This helped me clear out 135,000 records in about a minute.

somedood
May 9th, 2009 at 11:04 am

Thanks, this made my night.

Tom
August 6th, 2011 at 7:05 pm

Robbie, you work for Pentagon? xD Btw: nice one with MySQL command :)

February 8th, 2012 at 2:40 am

beautiful work – scheduled this with my wordpress install.

Andy
July 27th, 2012 at 2:25 pm

Perfect solution

Prathmesh
April 23rd, 2013 at 3:50 pm

Thanks

jsb
January 6th, 2014 at 8:07 pm

delete from mytable where datediff(now(), date) > 30;

mytable is the name of your table
date is the name of the column holding the timestamp (though this is bad form since date is a reserved word)

Blah
March 9th, 2016 at 10:54 pm

This doesn’t work; it lists records that I added 2 minutes ago.

LEAVE A COMMENT

theme by teslathemes