Automatically Cleaning Up Your ColdFusion Temp Files
November 7th, 2006
I build the following into a scheduled task recently to cleanup my CF temporary files. I put files here when they’re uploaded, being processed, or temporary attachments that are being mailed out. Because of my mail spooler settings, I’d rather not CFFILE DELETE them immediately, but rather a day or so later.
1 2 3 4 5 6 7 8 9 10 11 12 | <cfdirectory name="tmpFiles" action="list" directory="#GetTempDirectory()#"> <cftry> <cfloop query="tmpFiles"> <cfif DateDiff('d',tmpFiles.datelastmodified,now()) gt 1> <cffile action="delete" file="#GetTempDirectory()##tmpFiles.name#"> </cfif> </cfloop> <cfcatch type="any"> <cfset variables.return_code = 1> </cfcatch> </cftry> |
I use return_code elsewhere to report on the status of tasks; you can obviously setup any time of error handling you like. Simple code that helps me make sure that it gets cleaned up.
