Astra

Excel VBA: Delete Files in Folders and Subfolders

Estimated reading: 5 minutes

In this tutorial, we will learn to delete files in folders and subfolders using Excel VBA. Here we will demonstrate several ways to delete files in folders and subfolders. 

Code 01: Delete Files in Folders and Subfolders Using the Shell Command

The user may select a specific folder and delete all of the files in that folder and any subfolders inside that folder by using the following code. 

Sub DeleteFilesUsingShellWithDialog()
    ' Declare a variable to hold the selected folder path
    Dim f_Path As String
    
    ' Use the FileDialog object to display a dialog box and get the selected folder path
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a folder to delete files and subfolders from"
        .Show
        If .SelectedItems.Count > 0 Then
            f_Path = .SelectedItems(1)
        Else
            ' If the user did not select a folder, exit the subroutine
            Exit Sub
        End If
    End With
    
    ' Use the Shell function to run a command to delete all files in the folder and its subfolders
    Shell "cmd /c del """ & f_Path & "\*.*"" /s /q"
End Sub

Note: You should check the following references, before running the code.

vba delete files in folder and subfolders

Result:

  • After running the code, a folder picker dialogue box will open to select a folder.
  • Select a folder. Then all files are deleted from the folder and subfolder.

Code 02: Delete Files in Folders and Subfolders Using the File System Object

The following code will delete all the files in a folder and subfolders within it.

Sub DeleteFilesUsingFileSystemObject()
    ' Create a new FileSystemObject
    Dim file_sys_obj As Object
    Set file_sys_obj = CreateObject("Scripting.FileSystemObject")
    
    ' Set the path of the folder to delete files from
    Dim f_Path As String
    f_Path = "C:\Users\USER\Downloads\SOLVED EXCEL\"
    
    ' Loop through all files in the folder and delete them
    Dim file As Object
    For Each file In file_sys_obj.GetFolder(f_Path).Files
        file.Delete
    Next file
    
    ' Loop through all subfolders in the folder and delete files in them
    Dim subfolder As Object
    For Each subfolder In file_sys_obj.GetFolder(f_Path).SubFolders
        For Each file In subfolder.Files
            file.Delete
        Next file
    Next subfolder
End Sub

Code 03: Delete Files in Folders and Subfolders Using the Kill Function

Sub DeleteFilesUsingKillFunction()
    ' Declare variables
    Dim file_sys_obj As Object
    Dim mainfolder As Object
    Dim subfolder As Object
    Dim file As Object
    
    ' Set the path to the folder to be cleaned up
    Dim mainfolderPath As String
    mainfolderPath = "C:\Users\USER\Downloads\SOLVED EXCEL\"
    
    ' Create a new FileSystemObject
    Set file_sys_obj = CreateObject("Scripting.FileSystemObject")
    
    ' Get the folder object for the specified path
    Set mainfolder = file_sys_obj.GetFolder(mainfolderPath)
    
    ' Loop through all the files in the folder and delete them
    For Each file In mainfolder.Files
        Kill file.Path
    Next file
    
    ' Loop through all the subfolders in the folder and delete their files
    For Each subfolder In mainfolder.SubFolders
        For Each file In subfolder.Files
            Kill file.Path
        Next file
    Next subfolder
    
    ' Set all the objects to Nothing to free up memory
    Set file = Nothing
    Set subfolder = Nothing
    Set mainfolder = Nothing
    Set file_sys_obj = Nothing
End Sub

Example: Delete All Files by the Partial File Name or the File Extension in Folder and Subfolders Using VBA

The following code will allow the user to input the partial file name or file extension to delete any specific files.

' This sub prompts the user to enter a partial file name or file extension, and
' then calls the DeleteFiles sub to delete files that match the entered value
Sub Delete_Files_by_file_ext_or_partial_file_name()

    ' Declare variables for the user input and folder path
    Dim User_input As String
    Dim f_Path As String

    ' Set the folder path to the Downloads folder of the current user
    f_Path = "C:\Users\USER\Downloads\SOLVED EXCEL\"

    ' Prompt the user to enter a partial file name or file extension
    User_input = InputBox("Enter partial file name or file extension")

    ' Call the DeleteFiles sub with the folder path and user input
    DeleteFiles f_Path, User_input

End Sub


' This sub deletes files in a specified folder and its subfolders that match
' either a specified file extension or a partial file name
Sub DeleteFiles(ByVal f_Path As String, ByVal userInput As String)

    ' Create a FileSystemObject to work with files and folders
    Dim file_sys_obj As Object
    Set file_sys_obj = CreateObject("Scripting.FileSystemObject")

    ' Get the folder object for the specified folder path
    Dim folder_obj As Object
    Set folder_obj = file_sys_obj.GetFolder(f_Path)

    ' Loop through each file in the folder
    Dim file_obj As Object
    For Each file_obj In folder_obj.Files

        ' Check if the file extension matches the userInput or if the file name
        ' contains the userInput
        If LCase(Right(file_obj.Name, Len(userInput))) = LCase(userInput) Or InStr(1, LCase(file_obj.Name), LCase(userInput), vbTextCompare) > 0 Then

            ' If the file matches the specified file extension or partial file name,
            ' delete the file
            file_sys_obj.DeleteFile file_obj.Path, True

        End If
    Next file_obj

    ' Loop through each subfolder in the folder and call the DeleteFiles sub
    ' recursively with the subfolder path and userInput
    Dim subFolder_obj As Object
    For Each subFolder_obj In folder_obj.SubFolders
        DeleteFiles subFolder_obj.Path, userInput
    Next subFolder_obj

End Sub

Result: Suppose we have several files in a folder and we want to delete all Excel files from that folder.

vba delete files in folder and subfolders2

After running the code by pressing F5, an input box will appear.

vba delete files in folder and subfolders3

Enter the file extension for the Excel files (.xls) and press OK.

vba delete files in folder and subfolders 4

Then all the files with the “.xls” extension will be deleted.

vba delete files in folder and subfolders5

3 thoughts on “Excel VBA: Delete Files in Folders and Subfolders”

  1. I like the valuable information you provide in your articles.
    I will bookmark your blog and check again here frequently.
    I am quite sure I’ll learn many new stuff right here!
    Best of luck for the next!

    my web-site :: Marian

  2. I like the helpful information you provide in your articles. I’ll bookmark your blog and check again here regularly. I’m quite certain I’ll learn plenty of new stuff right here! Good luck for the next!

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc
Jump to Topics
SOLVED EXCEL
Scroll to Top