Introduction
When automating file organization and management with VBA, you often encounter situations where you need to “cleanly delete a folder along with all its contents.” This is particularly useful for clearing out temporary working folders or removing old backup directories.
By using the VBA FileSystemObject (FSO), you can easily handle such folder deletion tasks.
In this article, I will explain how to use the FSO DeleteFolder method to bulk delete a specified folder, including all files and subfolders inside it, along with important safety precautions.
⚠️ CAUTION: The code introduced in this article permanently deletes the folder. Deleted folders and files cannot be restored from the Recycle Bin. Please ensure the target folder path is correct before execution.
VBA Sample Code to Delete a Folder and Its Contents
In this example, we will create code to delete a folder named “TempWorkFolder” (and all its contents) located in the same directory as the Excel file.
Complete Code
Sub DeleteTargetFolder()
'== Define Variables ==
Dim fso As Object
Dim targetFolderPath As String
'== Create FileSystemObject ==
Set fso = CreateObject("Scripting.FileSystemObject")
'== Set the folder path to be deleted ==
' Note: We look for "TempWorkFolder" in the same directory as this workbook
targetFolderPath = ThisWorkbook.Path & "\TempWorkFolder"
'== Execute deletion only if the folder exists ==
If fso.FolderExists(targetFolderPath) Then
'== Ask the user for final confirmation ==
If MsgBox("The following folder will be permanently deleted. Are you sure?" & _
vbCrLf & vbCrLf & targetFolderPath, _
vbQuestion + vbOKCancel, "Final Confirmation") = vbCancel Then
MsgBox "Operation cancelled.", vbInformation
Exit Sub
End If
'== Delete the folder ==
' The second argument 'True' forces deletion even if read-only
fso.DeleteFolder targetFolderPath, True
MsgBox "Folder deletion complete.", vbInformation
Else
MsgBox "The target folder was not found.", vbExclamation
End If
'== Release Object ==
Set fso = Nothing
End Sub
Code Explanation
Here is a breakdown of the key parts of the code:
Set fso = CreateObject("Scripting.FileSystemObject")
This is the standard line to enable file system operations in VBA. It creates an instance of the FileSystemObject.
targetFolderPath = ThisWorkbook.Path & "\TempWorkFolder"
We store the path of the folder we want to delete in the targetFolderPath variable. In this case, we target “TempWorkFolder” in the same location as the macro file. This is the most critical part; verify your path carefully to avoid deleting the wrong folder.
If fso.FolderExists(targetFolderPath) Then ...
We use the FolderExists method to check if the target folder actually exists. Trying to delete a non-existent folder will cause an error, so this check is essential for stability.
If MsgBox(...) = vbCancel Then ...
Before permanently deleting the folder, we display a message box (MsgBox) to get final confirmation from the user. This acts as a safety mechanism to prevent accidental deletion due to operational errors. If “Cancel” is pressed, the process stops.
fso.DeleteFolder targetFolderPath, True
This single line executes the actual deletion.
- 1st Argument (
targetFolderPath): Specifies the path of the folder to delete. - 2nd Argument (
True): When set toTrue, it forces the deletion of files even if they have the “Read-Only” attribute. If omitted or set toFalse, read-only files will cause an error. Use this according to your needs.
Summary
In this article, I explained how to completely delete a folder and its contents using VBA’s FSO.
- Use the
DeleteFoldermethod to delete folders. - Always use
FolderExiststo check if the folder exists before attempting deletion. - It is strongly recommended to include a user confirmation step to prevent accidental data loss.
While this function is very powerful and convenient, misuse can lead to the loss of important data. Please pay close attention to the path specification and fully test the code on dummy folders before using it in actual business operations.
