HOWTO · VBA

How to Delete File in VBA

Learn how to delete one file, files by extension, or an empty folder with VBA.

On this page

Use VBA’s Kill statement to delete a file or matching files. For one file, FileSystemObject offers the DeleteFile method and the FileExists method for checking its path first. Use RmDir to remove an empty folder. Choose the section for your task and verify the path and target files before running deletion code; these operations remove files or folders.

Delete File in VBA

The right approach depends on what you need to remove. Kill is a VBA statement that accepts a file path or wildcard pattern. FileSystemObject is a separate object model for working with files and folders; its DeleteFile and FileExists members support deletion and path checks. RmDir removes an empty folder. These are distinct choices, so Kill is not a FileSystemObject function.

Before adapting an example, replace its sample path with the full path you intend to use. Check the folder and file name carefully. A wildcard can target multiple files in that folder, so inspect the intended matches before running bulk deletion code. The VBA snippets below are syntax examples, not Windows/VBA runtime tests: the supplied execution evidence covers only the PowerShell filesystem illustration, and contains no observed result for a VBA host.

Delete a Single File in VBA

The Kill statement is a short option for a known file. This complete procedure deletes the workbook at the specified path when run in a VBA host:

Sub DeleteFileWithKill(): Kill "D:\Test\testFile.xlsx": End Sub

Replace the sample path with the exact file you intend to remove. Kill does not ask for confirmation, so check the path before running it. The example assumes that the file exists; if it does not, Kill raises a VBA run-time error. Add deliberate error handling if the procedure must treat an absent file as a normal outcome.

You can also delete one file through FileSystemObject. Create the object, set the path, and call its DeleteFile method:

Sub DeleteFileWithFSO(): Dim FileSysObj As Object: Dim FileToDel As String: Set FileSysObj = CreateObject("Scripting.FileSystemObject"): FileToDel = "D:\Test\testFile.xlsx": FileSysObj.DeleteFile FileToDel, True: End Sub

The second argument, True, allows a read-only file to be deleted. Change it to False if you want read-only files to be preserved. This late-bound example uses CreateObject, so it does not require adding a project reference to the Scripting Runtime.

When the file may be absent, check it with FileExists before calling DeleteFile. The following procedure displays a message for either outcome and skips deletion when the path is not found:

Sub DeleteFileAfterChecking()
Dim FileSysObj As Object
Dim FileToDelete As String
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
FileToDelete = "D:\Test\testFile.xlsx"
If FileSysObj.FileExists(FileToDelete) Then
FileSysObj.DeleteFile FileToDelete, True
MsgBox "File Deleted"
Else
MsgBox "There is no file with the name you provided!"
End If
End Sub

FileExists checks the path before the procedure calls DeleteFile; it does not prevent another process from changing the file between those operations. If you need to handle access or deletion failures, add error handling appropriate to your workbook rather than assuming the check guarantees deletion.

This standalone PowerShell fixture demonstrates creating and removing a temporary file. It is a filesystem illustration, not VBA code or evidence of behavior in an Office host.

$path = Join-Path ([System.IO.Path]::GetTempPath()) ("vba-delete-demo-" + [guid]::NewGuid().ToString() + ".txt")
Set-Content -LiteralPath $path -Value "sample"
Remove-Item -LiteralPath $path

Delete All Text Files in VBA

To remove text files from one folder, use a wildcard path. D:\Test\Dst\*.txt matches .txt files directly in that folder; it does not search subfolders. This complete procedure passes that pattern to Kill:

Sub DeleteTxtFile(): Kill "D:\Test\Dst\*.txt": End Sub

Review the folder and its matching file names before running the procedure. Every matching text file is a target. The pattern does not search subfolders. If the folder contains no matching .txt file, Kill raises a VBA run-time error, so check the match set or add error handling when that is a possible state. Narrow the directory or change the extension only after confirming the intended set, and keep unrelated files outside the selected folder when possible. These outcomes follow VBA’s documented operation semantics; no Windows/VBA execution result was observed for this example.

Delete All Excel Files in VBA

To target .xlsx workbooks in one folder, use the corresponding wildcard path. The complete procedure targets matching workbooks directly in that folder, not every workbook in a directory tree:

Sub DeleteXlFile(): Kill "D:\Test\Dst\*.xlsx": End Sub

Check the folder and matching workbook names before running this bulk deletion. The .xlsx pattern does not include other Excel formats such as .xls or .xlsm; use a separate, deliberate pattern if those formats are also intended. Keep the directory narrow so the wildcard selects only files you mean to remove. As with the text-file pattern, no matching workbook causes Kill to raise a VBA run-time error; this behavior is described from VBA semantics and was not exercised in the supplied runtime evidence.

Delete a Folder in VBA

Use RmDir to remove a folder that is empty. This complete procedure names the sample directory:

Sub DeleteFolder(): RmDir "D:\Test\Dst": End Sub

RmDir does not recursively remove files or subfolders inside a nonempty folder. It requires an empty folder; calling it while files or subfolders remain raises a VBA run-time error. Move or delete its contents separately, then call RmDir for the folder itself. Verify that the path names the intended directory before running the procedure. This is the documented VBA behavior, but no Windows/VBA runtime result was observed for an empty-folder or nonempty-folder call.