Remove-MgUserMessage

What is Remove-MgUserMessage?

Remove-MgUserMessage is a Microsoft Graph PowerShell cmdlet used to permanently delete email messages from a user’s mailbox. The message is identified using its unique MessageId and is removed from the specified user’s Outlook mailbox.

This cmdlet directly interacts with Microsoft Graph and bypasses client-side Outlook rules.

🚀 Community Edition Released!

Try the M365Corner Microsoft 365 Reporting Tool — your DIY pack with 20+ out-of-the-box M365 reports for Users, Groups, and Teams.

Why Use Remove-MgUserMessage?

Administrators use Remove-MgUserMessage to:

  • Clean up unwanted or malicious emails
  • Automate mailbox hygiene tasks
  • Remove phishing or spam emails tenant-wide
  • Enforce compliance or retention policies
  • Perform bulk deletions based on conditions (date, folder, etc.)

It is especially useful in security response and mailbox automation scenarios.


Prerequisites

Before using this cmdlet, ensure:

  • Microsoft Graph PowerShell module is installed
  • You are connected to Microsoft Graph
  • Required permission scope is granted
Connect-MgGraph -Scopes "Mail.ReadWrite"

How to use Remove-MgUserMessage?

Basic syntax:

Remove-MgUserMessage -UserId <String> -MessageId <String>
  • UserId: User Principal Name (UPN) or User ID
  • MessageId: Unique ID of the email message

Remove-MgUserMessage Examples

  • Remove a Single Message by ID
  • Remove-MgUserMessage -UserId "user@domain.com" -MessageId "AAMkADhZD3KZAAA="

    Deletes the specified email message from the user’s mailbox.

  • Remove Multiple Messages
  • $messages = @("AAMkADhZD3KZAAA=", "AAMkADhZD3KZAAQ=")
    foreach ($messageId in $messages) {
        Remove-MgUserMessage -UserId "user@domain.com" -MessageId $messageId
    }
                                                

    Note:
    Use the Get-MgUserMessage cmdlet to retrieve message IDs.

  • Remove All Messages in a Folder
  • $folderMessages = Get-MgUserMessage -UserId "user@domain.com" `-MailFolderId "AQMkADhZD3KZAAAA=" -All
    
    foreach ($message in $folderMessages) {
        Remove-MgUserMessage -UserId "user@domain.com" -MessageId $message.Id
    }
                                               

    Note:
    Use Get-MgUserMailFolder to retrieve the MailFolderId.

  • Remove Messages Based on a Condition
  • $oldMessages = Get-MgUserMessage -UserId "user@domain.com" ` -Filter "receivedDateTime lt 2023-01-01"
    
    foreach ($message in $oldMessages) {
        Remove-MgUserMessage -UserId "user@domain.com" -MessageId $message.Id
    }
                                                

    Deletes messages received before a specific date.


Summary

Key Point Details
Cmdlet Name Remove-MgUserMessage
Purpose Permanently deletes emails from a mailbox
Required Scope Mail.ReadWrite
Target User mailbox messages
Primary Parameters UserId, MessageId
Automation Benefit Enables bulk and conditional email deletion
Common Use Case Phishing cleanup, mailbox hygiene, compliance actions

Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.

Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.

© Your Site Name. All Rights Reserved. Design by HTML Codex