[Dbix-class] Removing rows older than a given number of minutes ...

Rob Kinyon rob.kinyon at gmail.com
Sat Jan 23 22:33:22 GMT 2010


On Sat, Jan 23, 2010 at 16:28, Kiffin Gish <kiffin.gish at planet.nl> wrote:
> I want to delete all rows with a given state which have not been
> modified for a given time in seconds.
>
> my @rows = rs->search({ state => $state }, { last_modified => ??? });
> $_->delete for (@rows);
>
> How can I best do this?

$rs->search({
    state => $state,
    last_modified => [ "< TIMEDIFF( NOW(), ? SECONDS", $seconds ],
})->delete_all;

Standard SQL::Abstract stuff, described in both the DBIC cookbook and
the SQL::Abstract docs.

Note - "$rs->search()->delete_all" and "$_->delete for $rs->search()"
behave differently depending on if you have defined any DBIC-level
triggers. If you haven't, then delete_all is more efficient as it will
issue one SQL statement vs. N statements the way you were doing it. It
also better describes your intent.

Rob



More information about the DBIx-Class mailing list