Hey Guys,

I know there has to be a way to automatically delete a row in a MySQL table on a specified date. I am just brain dead today :icon_confused:

Let's just say I have CMS that I input posts and want one of the fields in the table to be "delete_date" and if I put a date in that field it would delete that row on the date specified. If I do not enter a date it would not auto delete.

What would be the best way to do this using PHP/MySQL. Tips or examples would help, maybe even tutorial links.

Any help will be greatly appreciated :)

Thanks,

Drew

Recommended Answers

All 5 Replies

One solution could be to create a trigger but I am not sure that it is what it is needed in your case. As I understood you want to predefine the date that the row will be deleted and not be deleted until that day. So the other way is to create a cron job that will run every day (the first second of each day) and make a simple delete based on the date you specified.

Whats the reasoning behind hard deleting the row in the first place?

I have several new posts about promotions that are date sensitive. So many different events, orientations, etc that I need a way to automate the removal of these items sitewide.

Thanks

Whats the reasoning behind hard deleting the row in the first place?

Instead of hard deleting them would it not be easier and more effective to just limit the display of them by the "delete_date" parameter?

So when displaying posts only display posts where delete_date < NOW()?

Then you have no time priority to physically remove the items from your database and could have a database "clean-up" routine that can be run manually whenever you are logged in, or maybe runs anytime a new post is made so you don't need a cron script etc to do it?

commented: Awesome! +2

Thanks for the response, I believe you are right on the money. That seems to be the best way to execute this :)

Instead of hard deleting them would it not be easier and more effective to just limit the display of them by the "delete_date" parameter?

So when displaying posts only display posts where delete_date < NOW()?

Then you have no time priority to physically remove the items from your database and could have a database "clean-up" routine that can be run manually whenever you are logged in, or maybe runs anytime a new post is made so you don't need a cron script etc to do it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.