Mirror

How to automatically delete records in a database after 30 days (Views: 100)


Problem/Question/Abstract:

Can anyone suggest an elegant way of automatically deleting database records if they are more than say 30 days old?

Answer:

Use a TQuery. Replace Table1.Tablename with your Tablename and replace FieldDate with the Date Field.

var
  MyDate: TDateTime;
begin
  MyDate := Date - 30;
  Query1.Active := False;
  Query1.SQL.Clear;
  Query1.SQL.Add('DELETE FROM "' + Table1.TableName + '"');
  Query1.SQL.Add('WHERE (FieldDate = "' + FormatDateTime('mm/dd/yyyy', MyDate) + '")');
  Query1.Active := True;
end;

<< Back to main page