VBA Delete All Files in Folder and Subfolders in Excel

VBA Delete Files in Folder and Subfolder

VBA Delete All Files in Folder and Subfolders in Excel. In the following tutorial we are using recursive method. It is nothing but looping through all folders until finishes all available folders. Let us see an example macro VBA code to delete files in folders and sub folders. You can also find the step by step instructions to run VBA code in the visual basic editor(VBE) window.

VBA Delete All Files in Folder and Sub-folders in Excel

Let us see an example macro to Delete All files from a folder and Sub-folders using VBA in Excel. The following is a main procedure. In this procedure we are calling sub procedure or sub routine. The sub-procedure helps us to delete files from folder and sub-folders. You can change the defined root folder ‘C:\VBAF1’ as per your requirement.

'Main procedure to delete all files in folder and subfolders
Sub VBAF1_All_Delete_Files_in_Folder_and_Subfolders()
    
     'Variable declaration
    Dim sFolderPath As String
    
    'Define Root Folder Path. You can change this path
    sFolderPath = "C:\VBAF1"
    
    'Çall Function
    Call VBAF1_All_Delete_Files_in_Folder_and_Subfolders(sFolderPath)
     
End Sub

Output: You can find following output screenshot for your reference.
VBA Delete Files in Folder and Subfolders

Sub-procedure to Delete All Files in Folder and Subfolders

You can find following Sub procedure to Delete All Files in Folder and Sub-folders using VBA in Excel. Here we have followed recursive procedure. We are looping through all sub-folders to delete available files from it. You can refer comments in the below VBA procedure.

'Subprocedure to Delete All Files in Folder and Subfolders
Sub VBAF1_Function_To_Delete_All_Files(sFldPath As String)
    
    'Variable declaration
    Dim sFolderPath As String
    Dim sFileName As String, oFile As Object, oFolder As Folder
        
    'Check for slash
    If Right(sFldPath, 1) <> "\" Then sFldPath = sFldPath & "\"
        
    'Create FSO Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(sFldPath)
    
    'Check Specified Folder exists or not
    If oFSO.FolderExists(sFldPath) Then
        'Loop through each file in a specified folder
        For Each oFile In oFolder.Files
            If oFile.Name <> ThisWorkbook.Name Then
                'Delete file
                oFile.Delete
            End If
        Next
     End If
     
    'Check Subfolders in specified folder
    For Each oSubFolder In oFolder.SubFolders
        'Recursive metod
        'Loop through all subfolders
        VBAF1_Function_T0_Delete_All_Files (sFldPath & oSubFolder.Name)
    Next
    
End Sub

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel VBA Tables and ListObjects

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Leave a Comment