Share via


Remove all PST from the Outlook Mailbox using VBScript

If you would like to remove all of the Personal Folders file .PSTs attached to the Outlook Mailbox profile then we can use RemoveStore Method.

Here is a sample VBScript to perform the job for us:

NOTE: Following programming examples is for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This sample code assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. This sample code is provided for the purpose of illustration only and is not intended to be used in a production environment.

 'Sample script to remove Personal Folders files (.pst) from the current MAPI profile or session
 RemoveAllPST
  
 Sub RemoveAllPST()
 Dim objOL 'As New Outlook.Application
 Dim objFolders 'As Outlook.MAPIFolders
 Dim objFolder 'As Outlook.MAPIFolder
 Dim i 'As Interger
 Dim strPrompt 'As String
  
 Set objOL = CreateObject("Outlook.Application")
 Set objFolders = objOL.Session.Folders
 For i = objFolders.Count To 1 Step -1
 On Error Resume Next
     Set objFolder = objFolders.Item(i)
      
     'Prompt the user for confirmation
     If (InStr(1, objFolder.Name, "Mailbox") = 0) And (InStr(1, objFolder.Name, "Public Folders") = 0) Then
     
     strPrompt = ""
     strPrompt = "Are you sure you want to remove " & objFolder.Name
    
     If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
         objOL.Session.RemoveStore objFolder
     End If
     End If
 Next
  
 End Sub

We can also refer to the article mentioned below related to RemoveStore Method:

NameSpace.RemoveStore Method

https://msdn.microsoft.com/en-us/library/bb219923.aspx

Hope this helps.

Comments