Search code examples
azuremicrosoft-graph-api

How to empty the "Deleted Items" folder, including non-mail items, using Microsoft Graph API?


I am trying to empty the "Deleted Items" folder in a user's mailbox using the Microsoft Graph API. However, I am encountering some challenges:

I attempted to use the permanentDelete endpoint:

POST https://graph.microsoft.com/beta/me/mailFolders/{folderId}/permanentDelete

But it returned the following error:

{
    "error": {
        "code": "ErrorDeleteDistinguishedFolder",
        "message": "Distinguished folders cannot be deleted."
    }
}

This indicates that system folders like "Deleted Items" cannot be deleted directly.

The reason I want to empty the "Deleted Items" folder is that it contains non-mail items (e.g., calendar events, tasks, contacts, etc.), which are not returned by the /messages endpoint:

GET https://graph.microsoft.com/v1.0/me/mailFolders/{folderId}/messages

These non-mail items still contribute to the totalItemCount of the folder, and I have not found a way to remove them using the API.

What I have tried: Using the /messages endpoint to delete email messages one by one or in batches, which works but only handles mail items. Investigating if there's a separate endpoint for non-mail items like /events or /contacts for this folder, but there seems to be no direct way to access them under "Deleted Items."

My Question: Is there a way to delete all items, including non-mail items, in the "Deleted Items" folder using Microsoft Graph API? If this is not directly possible, what would be the best approach to achieve this programmatically?

Notes: I am aware that this folder cannot be deleted (Distinguished folders cannot be deleted), and I only wish to empty this folder


Solution

  • What I have tried: Using the /messages endpoint to delete email messages one by one or in batches, which works but only handles mail items. Investigating if there's a separate endpoint for non-mail items like /events or /contacts for this folder, but there seems to be no direct way to access them under "Deleted Items."

    For Contacts there is a way of getting, deleting (or restoring) contacts from any folder eg

    https://graph.microsoft.com/v1.0/me/contactfolders/deletedItems/contacts
    

    Note the folder endpoint doesn't work /contactfolders/deletedItems because this folder isn't a contacts folder.

    You can also get Messages from a contacts folder (it's rare for there to be messages in contacts but older clients did allow you to do this) eg

    https://graph.microsoft.com/v1.0/me/mailfolders/contacts/messages 
    

    For Calendar Items (Events) this doesn't work so there is no way of currently enumerating or restoring/deleting a calendar item. The same goes for Tasks but the Planner API is how you CRUD tasks in the Graph.

    Older API's like MAPI and EWS will allow you access to all the items in the DeletedItems folder. MFCMapi is a pretty useful tool for understanding this https://github.com/microsoft/mfcmapi/releases this allows you to see that raw level access (that Outlook client and the Graph don't anymore). Eg with this you can see and delete the deleted Meetings and Appointment that might be stored in that folder.