This guide shows how to use Excel VBA to permanently delete all files in a specified folder. It requires no external tools—only the VBA editor built into Excel. Before running the code, ensure the folder path is correct and that you have backed up any important data, as deleted files cannot be recovered through the Recycle Bin when using this method.
What You'll Need
- Microsoft Excel (any recent version with VBA support)
- Access to the folder containing the files you wish to delete
- Permission to delete files in that folder
- A backup of any important data in the target folder (strongly recommended)
Step-by-Step
- Open Excel and press ALT + F11 to open the VBA Editor.
- In the VBA Editor, click Insert > Module to create a new module.
- Copy and paste the following VBA code into the module window:
Sub DeleteAllFilesInFolder() Dim fso As Object Dim folderPath As String Dim file As Object ' Set the folder path - CHANGE THIS TO YOUR TARGET FOLDER folderPath = "C:\Your\Folder\Path\" ' Check if the folder exists If Dir(folderPath, vbDirectory) = "" Then MsgBox "Folder not found: " & folderPath, vbCritical Exit Sub End If ' Create FileSystemObject Set fso = CreateObject("Scripting.FileSystemObject") ' Loop through each file in the folder and delete it For Each file In fso.GetFolder(folderPath).Files On Error Resume Next file.Delete True ' True forces deletion of read-only files If Err.Number <> 0 Then Debug.Print "Failed to delete: " & file.Name & " - Error: " & Err.Description End If On Error GoTo 0 Next file ' Clean up Set fso = Nothing MsgBox "All files in " & folderPath & " have been deleted.", vbInformation End Sub - Modify the
folderPathvariable in the code to match the exact path of the folder you want to empty. For example, if your folder isD:\Temp\Reports\, change the line to:folderPath = "D:\Temp\Reports\" - Ensure the path ends with a backslash (
\). - Click anywhere inside the code and press F5 to run the macro, or close the VBA Editor and run it via Developer > Macros in Excel, select
DeleteAllFilesInFolder, and click Run. - A confirmation message box will appear when the operation is complete.
- Verify the folder is empty by opening it in File Explorer.
Common Mistakes
- Forgetting to back up files before running the macro—deletion is permanent and bypasses the Recycle Bin.
- Using an incorrect folder path, such as missing the trailing backslash or typing a typo, which can cause the macro to fail or delete from the wrong location.
- Running the macro from a network drive without proper permissions, leading to access denied errors.
- Not stopping the macro if an error occurs—always test on a copy of the folder first.
- Assuming the macro deletes subfolders—it only deletes files, not folders.
Tips
- Test the macro first on a folder containing only disposable or temporary files.
- Add a
MsgBoxprompt before deletion if you want a final confirmation:
If MsgBox("Delete all files in " & folderPath & "?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
Debug.Print in the Immediate Window (CTRL + G in VBA Editor) to log which files were deleted or failed.If file.DateLastModified < Date - 30 Then file.Delete True
Troubleshooting
- Macro does nothing or shows “Folder not found”: Double-check the folder path. Ensure there are no extra spaces and that the drive letter is correct. Use F2 in the VBA Editor to verify the string value.
- Permission denied errors: Close any programs that might be using files in the folder (e.g., Excel, Word, backup software). Run Excel as Administrator if needed.
- Some files not deleted: These may be open, locked, or have special attributes. The
Trueparameter infile.Delete Trueforces deletion of read-only files, but not locked files. - Macro is disabled: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and enable macros (use cautiously).
- Wrong folder deleted: Immediately stop using the drive and use file recovery software if available—act quickly to avoid overwriting.
Frequently Asked Questions
Will this delete subfolders and their contents?
No, this code only deletes files inside the specified folder. Subfolders and their contents are left untouched.
Can I undo the deletion?
No. Files deleted via VBA FileSystemObject.Delete are permanently removed and do not go to the Recycle Bin. Always back up first.
What if I want to delete only certain file types, like .tmp or .log?
Modify the loop to check the file extension:
If LCase(fso.GetExtensionName(file.Name)) = "tmp" Then file.Delete True
Is it safe to run this on a network drive?
Only if you have explicit permission and have verified the path. Test on a small, non-critical folder first.
Can I delete files from multiple folders with one macro?
Yes. Either call the same subroutine multiple times with different paths, or loop through an array of folder paths.
Why does my antivirus flag this macro?
Some antivirus programs flag VBA scripts that modify or delete files as potentially unwanted behavior. This is a false negative if you trust the source. Add an exception if needed, or run in a controlled environment.