Skip to main content

FileSystemObject.DeleteFile method

Table of contents
  1. Syntax
  2. Remarks
  3. Examples

Deletes a specified file.


object.DeleteFile filespec, [ force ]

The DeleteFile method syntax has these parts:

Part Description
object Required. Always the name of a FileSystemObject.
filespec Required. The name of the file to delete. The filespec can contain wildcard characters in the last path component.
force Optional. Boolean value that is True if files with the read-only attribute set are to be deleted; False (default) if they are not.


An error occurs if no matching files are found. The DeleteFile method stops on the first error it encounters. No attempt is made to roll back or undo any changes that were made before an error occurred.


The following code illustrates the use of the DeleteFile method.

Sub DeleteFileDemo()
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    'Delete file Sales.xlsx
    fso.DeleteFile "D:\Temp\Sales.xlsx"
    'Delete all files with XLSX extension in specified folder
    fso.DeleteFile "D:\Temp\*.xlsx"
    'Delete all files in specified folder
    fso.DeleteFile "D:\Temp\*.*"
    'Delete all files in subfolders of D:\Temp
    fso.DeleteFile "D:\Temp\*\*.*"
End Sub

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>