Delete files using the Kill statement

In most common cases deleting a file is simple ...

Kill "C:\temp\file1.txt"

If the file does not exist you will get error 53 File not found. If this may be the case you can check its existence first:

Dim strFile  As String: strFile = "C:\temp\x.txt"
If Len(Dir$(strFile)) > 0 Then Kill strFile

you can also delete multiple files with one Kill statement using multiple-character (*) and single-character (?) wildcards.

Kill "C:\temp\*.txt"

Finally, Kill does not delete read-only file. If this may be the case, first change the file property to normal.

If Len(Dir$(strFile)) > 0 Then
    SetAttr strFile, vbNormal
    Kill strFile
End If

Code fragments for the above approaches are included in the Code VBA add-in, see below.

delete file