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.

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.

If the file doesn't exist, the following message will be displayed to the user:

Sorry, can't delete the file - it doesn't exist.

Syntax

Here's the full syntax of <cffile="delete">:

So it's a lot simpler than other cffile actions, such as upload or write, which accept more attributes.

Here's Adobe's documentation for cffile="delete" if you're interested.