• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Delete folders using a VBA Macro or PowerShell

Slipstick Systems

› Developer › Delete folders using a VBA Macro or PowerShell

Last reviewed on March 9, 2023     5 Comments

Many users have discovered that it is really slow to delete folders from Outlook. You need to:

  1. Select the folder.
  2. Click Delete.
  3. Click Ok that you really do want to delete the folder.
  4. Repeat for the next folder

Dragging folders to Deleted Items used to be faster (and still is, in older versions of Outlook): drag the folder to the Deleted items folder, or if you had a lot of folders to delete, drag to one of the folders you are going to delete (creating subfolders) then delete the 'parent' folder and answer the 'are you sure' dialog once. Now even that asks if you really want to delete the folder.

before and after deleting the folders

To speed it up you can use a macro or PowerShell.

The macro on this page runs on the selected folder, moving all subfolders to the deleted items folder. You'll need to select the Deleted items folder and run it again to permanently delete the folders.

The two PowerShell scripts delete subfolders of the Inbox (or subfolders of a specific folder) or all folders beginning with the same name, at the same level as the Inbox. They also delete the folders from the deleted items folder.

 

Use PowerShell to delete all subfolders

The first script deletes the subfolders under the Inbox, the second script permanently deletes all of the subfolders in the Deleted Items folder.

To change the default parent folder, change the folder number in this line (a list of common folder values is at the end of the powershell script listings):

$Folder= $MAPI.GetDefaultFolder(6)

Powershell instructions are below.

$olApp = new-object -comobject outlook.application
$namespace = $olApp.GetNamespace("MAPI")

# delete subfolders from Inbox folder
$Folder = $namespace.GetDefaultFolder(6)
$SubFolders = $Folder.Folders 
$count = $SubFolders.Count
$count
$SubFolders | % {$_.Delete()}
# End Inbox 

$olApp.Quit | Out-Null
[GC]::Collect()

Permanently Delete Folders

$olApp = new-object -comobject outlook.application
$namespace = $olApp.GetNamespace("MAPI")

# permanently  delete subfolders from Deleted items folder
$Folder = $namespace.GetDefaultFolder(3)
$SubFolders = $Folder.Folders 
$count = $SubFolders.Count
$count
$SubFolders | % {$_.Delete()}
# End permanently delete folders

$olApp.Quit | Out-Null
[GC]::Collect()

 

Delete folders in a secondary data file

To delete from a secondary or non-default data file in your profile, you need to identify the data file by name. This will be the name you see in the folder list, in most cases it will be your email address but may be Outlook Data file or Personal Folders.

$Folders = $namespace.Folders.Item("alias@domain.com")
then reference the folder by name:
$Folder = $Folders.Folders.Item("Folder name")

You'll run the same script twice, once with the folder you are deleting subfolders from, then for the deleted items folder.

$olApp = new-object -comobject outlook.application
$namespace = $olApp.GetNamespace("MAPI")
$Folders = $namespace.Folders.Item("alias@domain.com")

# delete subfolders from the parent folder
$Folder = $Folders.Folders.Item("Folder name")

# to permanently delete subfolders from Deleted items folder
#$Folder = $Folders.Folders.Item("Deleted Items")

$SubFolders = $Folder.Folders 
$count = $SubFolders.Count

$SubFolders | % {$_.Delete()}
# End permanently delete folders

$olApp.Quit | Out-Null
[GC]::Collect()

Delete subfolders by name or using wildcards

To delete a specific subfolder by name, use this line:

$SubFolder = $Inbox.Folders | Where-Object {$_.Name -eq "Notes_0"}

To change the default parent folder, change the folder number in this line:

$Folder= $MAPI.GetDefaultFolder(6)

To delete a group of subfolders by a matching string in the folder name, use the -like operator. In addition, this sample shows how to delete folders at the same level as Inbox.

$SubFolder = $Inbox.Parent.Folders | Where-Object {$_.Name -like "Notes_0*"}

To use, you can copy and paste the code in a new powershell window and press Enter. Powershell instructions are below.

paste the powershell in the window

This version of the PowerShell script permanently deletes the folders after moving them to the Deleted folder. You may want to split it into two scripts.

$Outlook = New-Object -comobject "Outlook.Application"
$MAPI = $Outlook.getnamespace(“mapi”)
$Folder= $MAPI.GetDefaultFolder(6)

# Delete subfolders that begin with the same keyword
# This sample deletes folders at the same level as Inbox
$SubFolder = $Folder.Parent.Folders | Where-Object {$_.Name -like "Notes_0*"}
$SubFolder | % {$_.Delete()}
# End Delete folders

# permanently  delete subfolders from Deleted items folder
$Deleted = $MAPI.GetDefaultFolder(3)
$SubFolder = $Deleted.Folders | Where-Object {$_.Name -like "Notes_0*"}
$SubFolder | % {$_.Delete()}
# End permanently delete folders

$olApp.Quit | Out-Null
[GC]::Collect()

NameValueDescription
olFolderCalendar9Calendar folder.
olFolderContacts10Contacts folder.
olFolderDeletedItems3Deleted Items folder.
olFolderDrafts16Drafts folder.
olFolderInbox6Inbox folder.
olFolderJunk23Junk Email folder.
olFolderNotes12Notes folder.
olFolderSentMail5Sent Items folder.
olFolderTasks13Tasks folder.

 

Delete specific folders using VBA

Use this macro to delete all folders that begin with the same name. Edit this line for the folder name and the character count as needed.
If Left(olTempFolder.Name, 6) = "Notes_" Then

To use, select the parent folder (can be the mailbox root, which may be your email address or 'Outlook Data File') then run the DeleteFolders macro.

Public Sub DeleteFolders()
Dim oFolder As Outlook.Folder

' Select the parent folder
Set oFolder = Application.ActiveExplorer.CurrentFolder
ProcessFolder oFolder

End Sub
Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)
         
    Dim i As Long
    Dim olNewFolder As Outlook.MAPIFolder
    Dim olTempFolder As Outlook.MAPIFolder
    Dim olTempFolderPath As String
     ' Loop through the items in the current folder.
    For i = CurrentFolder.folders.count To 1 Step -1
          
      Set olTempFolder = CurrentFolder.folders(i)
              Debug.Print olTempFolder
      If Left(olTempFolder.Name, 6) = "Notes_" Then
          
    ' prints the folder name only
             olTempFolder.Delete
     End If

    Next
     ' Loop through and search each subfolder of the current folder.
    For Each olNewFolder In CurrentFolder.folders
       
       ProcessFolder olNewFolder
          
    Next
End Sub

 

Delete all subfolders using a macro

To use this macro, select the parent folder then run the macro. It will delete all subfolders from the selected folder. Select the Deleted items folder to permanently delete the folders.

Macro instructions are below.

Public Sub DeleteFolders()

Dim oFolder As Outlook.folder

' Select the parent folder
Set oFolder = Application.ActiveExplorer.CurrentFolder
ProcessFolder oFolder

End Sub
Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)
         
    Dim i As Long
    Dim lCountOfFound As Long
    Dim olNewFolder As Outlook.MAPIFolder
    Dim olTempFolder As Outlook.MAPIFolder
    Dim olTempFolderPath As String
     ' Loop through the items in the current folder.
    For i = CurrentFolder.Folders.count To 1 Step -1
          
        Set olTempFolder = CurrentFolder.Folders(i)
          
        ' prints the folder name only
          Debug.Print olTempFolder
             olTempFolder.Delete
                   
        lCountOfFound = lCountOfFound + 1
          
    Next
     ' Loop through and search each subfolder of the current folder.
    For Each olNewFolder In CurrentFolder.Folders
          
         'Don't need to process the Deleted Items folder
        If olNewFolder.Name <> "Deleted Items" Then
            ProcessFolder olNewFolder
        End If
          
    Next
   MsgBox lCountOfFound
End Sub

 

How to use the macro on this page

First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.

To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.

After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.

The macros on this page should be placed in a module.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

To put the code in a module:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.
  3. Press F5 or the Run button to run the macro.

More information as well as screenshots are at How to use the VBA Editor

 

Using PowerShell Scripts

To use, right-click on the Start menu in Windows 10 and click on the Windows PowerShell entry. Paste the entire script in the PowerShell window and press Enter.
paste the powershell in the window

Note: This PowerShell script will not work with the Windows Store version of Office. You'll need to use the macro version if you have the Windows store version of Office installed.

Saving PowerShell Scripts

If you want to save the script as a .ps1 file, paste it into Notepad and save it with the extension .ps1. To open it in the PowerShell IDE, type powershell on the start menu and click on Windows PowerShell IDE when the PowerShell app is found. Paste the script in the editing window.

To use it, you need to allow local scripts by running this command:

Set-ExecutionPolicy RemoteSigned

To run your saved .ps1 file, right-click on the script and choose Run with PowerShell.

More Information

Add or Delete folders, using a list of folders in a text file: Create new Outlook folders using PowerShell
See OlDefaultFolders enumeration (Outlook) for the full list of folder enumerations.

Delete folders using a VBA Macro or PowerShell was last modified: March 9th, 2023 by Diane Poremsky

Related Posts:

  • Create new Outlook folders using PowerShell
  • Print a list of your Outlook folders
  • Empty Multiple Deleted Items Folders using a Macro
  • Open Outlook Folders using PowerShell or VBScript

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
5 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

dec
November 15, 2023 12:25 am

Hi there Diane

So is there any update/enhancement to just delete empty Folders?

Which would seem to be the obvious Use Case'

0
0
Reply
gabriel
January 30, 2023 1:50 pm

Hello, ¿how I can move mails massive of an folder a subfolder etc?

0
0
Reply
Torsten
October 21, 2022 6:18 am

Awesome work, thank you! This just saved me from having to manually delete 162 folders..

0
0
Reply
Bruce
August 17, 2022 12:51 am

I ran the following script on a secondary mailbox named "SortedMail". I tried running all the lines by pasting them in together and hitting Enter, and also by pasting them in one by one and hitting Enter after each line. Both approaches produced the same error message. The script was

$olApp = new-object -comobject outlook.application
$namespace = $olApp.GetNamespace("MAPI")
$Folders = $namespace.Folders.Item("SortedMail")
#$Folder = $Folders.Folders.Item("Deleted Items")
$SubFolders = $Folder.Folders 
$count = $SubFolders.Count
$SubFolders | % {$_.Delete()}
$olApp.Quit | Out-Null
[GC]::Collect()

The error message for the all-at-once case was

You cannot call a method on a null-valued expression.
At line:7 char:18
+ $SubFolders | % {$_.Delete()}
+         ~~~~~~~~~~~
  + CategoryInfo     : InvalidOperation: (:) [], RuntimeException
  + FullyQualifiedErrorId : InvokeMethodOnNull

In the one-by-one case, a slightly different error message appeared after the line

$SubFolders | % {$_.Delete()}

You cannot call a method on a null-valued expression.
At line:1 char:18
+ $SubFolders | % {$_.Delete()}
+         ~~~~~~~~~~~
  + CategoryInfo     : InvalidOperation: (:) [], RuntimeException
  + FullyQualifiedErrorId : InvokeMethodOnNull



0
0
Reply
Hans
December 19, 2019 9:06 am

Hi, the powershell option works like a treat for us. Is this also possible on an online archive in outlook? We hope someone has figured that one out as well and can give us the code.

0
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 36

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Use Classic Outlook, not New Outlook
  • How to Remove the Primary Account from Outlook
  • How to Hide or Delete Outlook's Default Folders
  • Adjusting Outlook's Zoom Setting in Email
  • This operation has been cancelled due to restrictions
  • Reset the New Outlook Profile
  • Syncing Outlook with an Android smartphone
  • Add Holidays to Outlook's Calendar
  • iCloud error: Outlook isn't configured to have a default profile
  • Online Services in Outlook: Gmail, Yahoo, iCloud, AOL, GoDaddy
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2026 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

:wpds_smile::wpds_grin::wpds_wink::wpds_mrgreen::wpds_neutral::wpds_twisted::wpds_arrow::wpds_shock::wpds_unamused::wpds_cool::wpds_evil::wpds_oops::wpds_razz::wpds_roll::wpds_cry::wpds_eek::wpds_lol::wpds_mad::wpds_sad::wpds_exclamation::wpds_question::wpds_idea::wpds_hmm::wpds_beg::wpds_whew::wpds_chuckle::wpds_silly::wpds_envy::wpds_shutmouth:
wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment