ColdFusion Delete File
You can use the cffile tag to delete a file from the server.
Example of Deleting a File
The following code deletes the file specified with the file attribute.
<cffile action="delete" file="C:\docs\accessLog.txt">
Checking that the File Exists
The above code will work properly if the file actually exists. If the file doesn't exist, the user will receive an error.
You can use the built in ColdFusion FileExists() function to check if the file exists. By using this as an expression in a cfif statement, you can determine what to do if the file doesn't exist.
<cfif FileExists("C:\docs\accessLog.txt")> <cffile action="delete" file="C:\docs\accessLog.txt"> <cfelse> <p>Sorry, can't delete the file - it doesn't exist.</p> </cfif>
If the file doesn't exist, the following message will be displayed to the user: