Skip to main content
February 5, 2026
Question

CFFILE not deleting "read-only" files

  • February 5, 2026
  • 2 replies
  • 32 views

I have a simple loop that is used to clean up old files within a directory:

​<cfdirectory action="list" name="qDir" directory="#variables.path#">
<cfloop query="qDir">
<cfif qDir.Type eq "File">
<cfif DateDiff("h", qDir.DateLastModified, Now()) gt attributes.keep_for_hours>
<cfset variables.full_file_path = variables.path & qDir.Name>
<cfif FileExists(variables.full_file_path)>
<cffile action="delete" file="#variables.full_file_path#">
</cfif>
</cfif>
</cfif>
</cfloop>

This is on Linux where ColdFusion is running as apache.
Recently a system scan process has been leaving behind files owned by root within the directory resulting in an error stating:
“The file or directory <filename> provided as the Source is read-only. - The Delete cannot be performed.”
While it’s true that the file is read-only, apache owns the directory the file is in and should still be able to delete the file.
How do I go about encouraging CFFILE to remove the file anyway?

    2 replies

    BKBK
    Community Expert
    Community Expert
    February 6, 2026

    Apparently, your ColdFusion installation does not have w+x permissions on the directory from which the read-only files are to be deleted. So, the error message is correct.

    Note that ColdFusion does not and cannot run “as Apache” on Linux. it always runs as its own Java Virtual Machine, that is, as a separate user in Linux, even when integrated with Apache.

    Charlie Arehart
    Community Expert
    Community Expert
    February 5, 2026

    While someone else may have a different take, I’d start with confirming your current assumptions about the user running CF, and then about the permissions on the file itself (rather than the folder).

     

    You can see both those from within CF with this code. Note there are two aspects of getting the file info, first literally getfileinfo (which confirms things like the readonly mode) but then using cfdirectory to get the permissions (offered via the filemode, only on Linux). I show both, with the latter using a Q of Q to focus on just a given file in a given folder (defined at the outset, in a var whose value you should change). 

     

    You could of course fold this into your own code (already looping over cfdirectory), but faster would be to just drop it in a template, point to the path/file in question, and see what it reports. :-) Otherwise, perhaps the code may help someone else in the future. (And I offered it as tags, since that’s what you used. A script fan can of course convert it to cfscript with Pete F’s sweet cfscript.me site.)

     

    Let us know what you find, even if only confirmation of your assumptions.

    <cfoutput>
    Account running cf: #server.system.properties["user.name"]#

    <cfset filename="/yourpath/yourfilename" >

    <p>GetFileInfo for #filename#:
    <cfdump var="#GetFileInfo(filename)#">
    </cfoutput>

    <cfif server.os.name does not contain "Windows">
    <!--- The file mode cannot be obtained using cfdirectory in Windows --->
    <cfdirectory directory="#getdirectoryfrompath(filename)#" name="cfdir" >

    <cfquery dbtype="query" name="getfile">
    select mode from cfdir
    where Name='#getfilefrompath(filename)#'
    </cfquery>

    <cfoutput>
    <p>File mode for #filename#: #getfile.mode#
    </cfoutput>
    </cfif>

     

    /Charlie (troubleshooter, carehart. org)