The FileSystemObject VBA DeleteFile function deletes a specified file or files. The functions allows you to use wildcards (*) to delete multiple files matching a specific file path pattern which saves a lot of time for easy deletion scenarios.
VBA DeleteFile Syntax
fso.DeleteFile( filename, [ force ] )
- filename – The location names of file(s) to delete. You can use wildcards such as *.* to specify more than a single file matching the pattern.
- force – Optional. If True only read-only files are deleted. If False any files are deleted. False is default.
Set fso = CreateObject("Scripting.FileSystemObject") 'Delete file Hello.xlsx fso.DeleteFile "c:\Src\Hello.xlsx" 'Delete all files with XLSX extension in C:\Src folder fso.DeleteFile "c:\Src\*.xlsx" 'Delete all files in specified folder fso.DeleteFile "c:\Src\*.*" 'Delete all files in subfolders of C:\Src fso.DeleteFile "C:\Src\*\*.*"