Drew
Drew

Reputation: 28

How do I automatically Delete Rows from MySQL Database on specific date?

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 : /

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.

Upvotes: 1

Views: 5593

Answers (3)

IAbstract
IAbstract

Reputation: 19881

I don't know PHP, but this is really a language agnostic question unless you don't know how to execute SQL in the language.

You can run a scheduled task, as @tylerl suggests. Easy to do in a .Net language, not sure about PHP. You also have the option of running a service if you want old records to be continually deleted throughout the day. Again, easy enough in .Net.

If, however, you have an application (PHP, .Net, etc) that you want to physically check for records to delete:

delete * from myTable where recordDateField > 'cutOffDate' // I think you pass parameters with a `$` - not sure

Upvotes: 0

horatio
horatio

Reputation: 1436

A basic query:

DELETE * FROM cms_table WHERE delete_date='$the_date_to_delete'

( see: http://dev.mysql.com/doc/refman/5.0/en/delete.html )

Upvotes: 0

tylerl
tylerl

Reputation: 30877

Run a scheduled task (cron job) which deletes all the rows where the delete_date field is greater than or equal to the current date.

Upvotes: 2

Related Questions