Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am designing an aggregator that stores published posts by members. But the posts older than 7 days must be deleted automatically or with cron.
Can any one give idea?

What I have tried:

this is designed in gregarius to I tried a plugin of gregarius
Posted
Updated 17-Nov-16 17:33pm
Comments
Mehdi Gholam 17-Nov-16 2:22am    
... and what have you tried?

1. Create an SQL script that deletes the records older than 4 days. Something like:
SQL
DELETE FROM table_name WHERE date_column < DateAdd(day, -4, GetDate())

2. Create an SQL job[^] which run this script at a scheduled time everyday.

Also you might want to ARCHIVE the data instead of deleting it unless it is absolutely useless.
 
Share this answer
 
v2
Comments
Richard Deeming 17-Nov-16 9:43am    
It would be better to use DateAdd[^] for the date calculation, rather than subtracting an integer:
... WHERE date_column < DateAdd(day, -4, GetDate())

Date arithmetic only works with the older datetime / smalldatetime types.
Ankur\m/ 6-Dec-16 10:15am    
You are absolutely correct! I mostly work with datetime datatype so have a habit of using it like this. I shall change that from now onwards. And I have also updated my solution. Thank you!
You can also build a DELETE statement via datediff and getdate functions.

SQL
DELETE FROM TableName WHERE DATEDIFF(day,getdate(),thatColumn) < -4
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900