How do I delete records 7 days back?


Trying to write a purge function that will remove all records older than 7 days.

Looked at $this::model()->deleteAll()

But am stumped.

Can someone help me?


did you try deleteAllByAttributes ?

'delete from searchlog where date BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()'

Trying to figure out the syntax for the above code when using deleteAll or deleteAllByAttributes.

try this (not tested):

$criteria=new CDbCriteria;

$criteria->addBetweenCondition("date","DATE_SUB(NOW(), INTERVAL 7 DAY)","NOW()")


Thank you, will test tonight.

This will not delete all records older than 7 days, but all records of the last 7 days…

To delete all records older than 7 days it must look like:

// delete from searchlog where date < DATE_SUB(NOW(), INTERVAL 7 DAY)

$criteria->addCondition("date < DATE_SUB(NOW(), INTERVAL 7 DAY)");

No between condition is needed.