Delete items from a public folder

Last reviewed on December 4, 2012

Use this code to delete the contents of a specific public folder.

To delete everything in the currently selected folder, use
Set delContents = Application.ActiveExplorer.CurrentFolder
instead of the Set delContents = olns.Folders line.

To use with deeper folder paths, you need to add more .Folders("MySubfolder"):
Set delContents = olns.Folders("Public Folders - alias@domain.com").Folders("All Public folders").Folders("Myfolder").Folders("MySubfolder").Folders("MySubfolder").Folders("MySubfolder")

Delete items from a folder

Public Sub DeleteItemsInFolder()
Dim delContents As Outlook.MAPIFolder
Dim item As Object
Dim entryID As String

' Replace to use array
Set olns = Application.GetNamespace("MAPI")

Set delContents = olns.Folders("Public Folders - alias@domain.com").Folders("All Public folders").Folders("Myfolder").Folders("MySubfolder")

For i = delContents.Items.Count To 1 Step -1
    delContents.Items(i).Delete    
Next
' end replace 

Set item = Nothing
Set delContents = Nothing
Set olns = Nothing

End Sub

Use an array to delete items from multiple subfolders

If you want to delete the contents of several subfolders, you can use an array to walk the folders. Replace the code above, from the Set olns line to Next, with this block. Enter the folder names in the Array, comma-separating the list.


Dim arrFolder As Variant

Set olns = Application.GetNamespace("MAPI")

arrFolder = Array("Contacts1", "Contacts2")

For F = LBound(arrFolder) To UBound(arrFolder)

Set delContents = olns.Folders("Public Folders - alias@domain.com").Folders("All Public folders").Folders("TestDelete").Folders(arrFolder(F))
For i = delContents.Items.Count To 1 Step -1
    delContents.Items(i).Delete
Next
 
Next F

More Information

To use a variable for the public folder path, so it works with any user's profile, see How to get reference to Public Folder Store using Outlook Object Model for Outlook 2010?

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999 and involved in IT support since 1985, Diane is the author of several books and 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.