SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete files older than n-days via T-SQL


Delete files older than n-days via T-SQL

Author
Message
Gordon-265412
Gordon-265412
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1224 Visits: 1188
Comments posted to this topic are about the item Delete files older than n-days via T-SQL

Your friendly High-Tech Janitor... Smile
joelau@toledoblade.com
joelau@toledoblade.com
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 270
The 'Forfiles' command can do this in one line of code. I don't know why we have to re-invent the wheel here.



Gordon-265412
Gordon-265412
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1224 Visits: 1188
Forfiles is not included in every os by default. I needed to make sure that the code was supportable with no additional external software requirements (resource kit executables and such).

The Forfiles command would make life quite a bit simpler and it could be easily added to it and the FOR command removed...

Your friendly High-Tech Janitor... Smile
Juan Carlos-264378
Juan Carlos-264378
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 126
Thanks for the script.
Months ago I was looking for a scritp like this one with an additional feature: recursively delete old files in the selected folder an sub-folders.
I suppose it is difficault to deal with sub-folders at any level using T-SQL and finally I use an external exe program written in C# (deleteold.exe from codeproject).
Anyway, I would prefere this functionality in T-SQL ;-)
Regards
mattyk6
mattyk6
SSC-Addicted
SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)

Group: General Forum Members
Points: 426 Visits: 49
Just curiuos if anyone is using this? I'm planning to implement this at a disaster recovery site. On our live SQL Server we back up the db and ftp it to another location. I figure that I can run this as a job at the DR site to delete the old backups.
Gordon-265412
Gordon-265412
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1224 Visits: 1188
The code presented is actually about six months older than the date on it - I've been using this method on around 150 servers since it's inception with no issues.



Edit:

FWIW, you can add a PRINT @DynDelete and if you schedule the code through SQLAgent you'll have the delete command output so you can record what is being deleted by having the job step append its output to a file. [My production version of this logs the deletes to a table]

Your friendly High-Tech Janitor... Smile
mattyk6
mattyk6
SSC-Addicted
SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)SSC-Addicted (426 reputation)

Group: General Forum Members
Points: 426 Visits: 49
thanks
Josep
Josep
Mr or Mrs. 500
Mr or Mrs. 500 (556 reputation)Mr or Mrs. 500 (556 reputation)Mr or Mrs. 500 (556 reputation)Mr or Mrs. 500 (556 reputation)Mr or Mrs. 500 (556 reputation)Mr or Mrs. 500 (556 reputation)Mr or Mrs. 500 (556 reputation)Mr or Mrs. 500 (556 reputation)

Group: General Forum Members
Points: 556 Visits: 4680
If you want to use SQL instead of other applications and you're using SQL Server 2005, why you don't use the extended procedure xp_delete_file?

Personally I feel it quite restricted and I still use a delete command line that I create for myself when I was using SQL Server 2000.
tsduke
tsduke
SSC-Addicted
SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)

Group: General Forum Members
Points: 471 Visits: 474
I'm trying it and getting all kinds of incorrect syntax error when parsing it.
Gordon-265412
Gordon-265412
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1224 Visits: 1188
It might be the extra characters that are 'added' to the script when posted.

Your friendly High-Tech Janitor... Smile
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search