0

I want to create something like Snapchat that will automatically delete row after 24 hours in php/mysql. I have read about cron jobs,but it slows down apps,so i was wondering if there is other way to do it?

  • 1
    You can use SQL in-built function triggers to achieve this. You can create a trigger which will execute in every 1 hours and will check all records of the table. If record created datetime is greater than 24 hours, it will delete the record as below: CREATE EVENT hourly_record_cleanup ON SCHEDULE EVERY 1 HOUR DO DELETE FROM messages WHERE createdtime <= DATE_SUB(NOW(), INTERVAL 24 HOURS); Hope it helps you. – Rohit Mittal Apr 18 '19 at 02:49
  • 1
    why re-asking same question? – NullPoiиteя Apr 18 '19 at 02:49

2 Answers2

1

Put the expiry date as a column in the table, and only query rows that haven't expired yet.

Gray
  • 2,333
  • 1
  • 19
  • 24
1

This works for me.

DELETE FROM snaps WHERE created_at<=DATE_SUB(NOW(), INTERVAL 1 DAY)