Delete files using the Kill statement
In most common cases deleting a file is simple ...
If the file does not exist you will get error 53 . 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.
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.