none
How can I delete folders from my mailboxes RRS feed

  • Question

  • Hello,

    I'm using an Exchange Server 2016 Version 15.1 Build 1034.26.

    I would like to delete the content of my mailboxes.

    I created a PS-script with search-mailbox -id -deletecontent -force.

    It delets all mails but the folders remain.

    What do I have to change that all folders get deleted too?

    Thanks

    Lukas

    Thursday, June 27, 2019 3:03 PM

Answers

  • On a second thought, if you want to delete the each and every folder from the mailbox why dont you just disable the mailbox and re-enable the same for the AD user, it should work as it's a practice mailbox.

    Disable-Mailbox User

    Enable-Mailbox User

    Don't delete the user from the AD, you will have a brand new mailbox for the user.

    In above screenshot, if you still need the script could you try printing the $Folder.DisplayName inside the loop so that we can check if there are any folders processed or not?

    • Marked as answer by WIFI Tirol Friday, June 28, 2019 1:15 PM
    Friday, June 28, 2019 11:34 AM

All replies

  • Hi Lukas,

    Yes, Search-Mailbox command could only delete emails, it cannot delete folders.

    If you want to delete a folder in someone's mailbox, you can give this mailbox's full access permission to your mailbox, then you will could delete their folders from your mailbox directly.

    If you want to delete folder with PowerShell, you may need to use EWS as DumbleD0re said, I will help you move this thread to a more appropriate forum: Exchange development forum

    Regards,

    Kyle Xu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, June 28, 2019 6:49 AM
  • Dear DumbleD0re,

    thanks for your help. I found this EWS thing yesterday but I couldn't believe it has to be as complicated.

    I am curious that nobody else needs this.

    We are a big educational insititution and our mailboxes are only for practising. So I need to delete the content or the mailboxes periodically.

    Maybe you know a different way to solve this task.

    But I tried the EWS script but it did nothing.

    I changed the $MailboxName, $dllpath and in the penultimate line I tried -eq "*" because I would like to delete all folders.

    I don't get any error but the folders remain.

    I know my programming skills are very poor.

    Do I have to change the Exchange2010_SP1 in the $service to something like Exchange2016 but what will be the correct expression?

    Thanks for all your help.

    Lukas

    $MailboxName = 'xy@domain.local'
    $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
    [void][Reflection.Assembly]::LoadFile($dllpath)
    $Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
    $Service.AutodiscoverUrl($MailboxName,{$true})
    $RootFolderID = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
    $RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$RootFolderID)
    $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
    $Response = $RootFolder.FindFolders($FolderView)
    $FolderView
    $RootFolder
    ForEach ($Folder in $Response.Folders) {
      if($folder.DisplayName -eq "*") {
        $folder.delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete) } }

    Friday, June 28, 2019 7:26 AM
  • Dear Kyle,

    I just answered to DumbleD0re.

    Your first suggestion doesn't work for us but thanks for the idea.

    How can we move the thread?

    Kind regards

    Lukas

    Friday, June 28, 2019 7:29 AM
  • Hi Lukas,

    I have help you move this thread to Exchange development forum. 

    Regards,

    Kyle Xu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, June 28, 2019 8:26 AM
  • Hi Lukas,

    Could you post the error that you are getting while executing the same?, also have you installed EWS 2.2 prior to running the script?

    Friday, June 28, 2019 10:45 AM
  • Hi,

    yes I installed EWS 2.2 prior to running the script, otherwise I wouldn't have the $dllpath and I don't get any error.

    Friday, June 28, 2019 11:11 AM
  • On a second thought, if you want to delete the each and every folder from the mailbox why dont you just disable the mailbox and re-enable the same for the AD user, it should work as it's a practice mailbox.

    Disable-Mailbox User

    Enable-Mailbox User

    Don't delete the user from the AD, you will have a brand new mailbox for the user.

    In above screenshot, if you still need the script could you try printing the $Folder.DisplayName inside the loop so that we can check if there are any folders processed or not?

    • Marked as answer by WIFI Tirol Friday, June 28, 2019 1:15 PM
    Friday, June 28, 2019 11:34 AM
  • Dear DumbleD0re,

    thanks a lot. The two cmdlets work perfect.

    Sometimes it is good to explain again what one would like to achieve then others can look for different solutions.

    Regards

    Lukas

    Friday, June 28, 2019 1:18 PM