What is the best approach to delete garbage and expired entries in a MySQL database?
Posted on 29 June 2010 by Abidoon
I want to delete users that hasn’t logged in in a certain amount of days, and other expired entries in a database. What is the best way of doing this automatically? I’m using MySQL and PHP for my web application.
One way I have thought is having a clean_db() function that I call in every page request. Other option can be programming a cron script that runs automatically at certain times.
What is the best way of cleaning a database?
Tags | Approach, Best, database, Delete, Entries, expired, garbage, MySQL

I’d go for a cronjob.
If you have many visitors, the amount of SQL connections might slow down your server.
Clean outdated records every page someone visits, is way to inefficient, unless you have hunderds of records expiring every hour.
As I guess this is not the case, I advise you to it with a cronjob.
You have to ask yourself: Is every page an extra SQL connection to check for expired records more effective then having a little bit garbage in your database for a couple of hours, as databases are made for storing loads of information.