Search code examples
azuremicrosoft-graph-api

How to Accurately Retrieve Deleted Items Folder Size and Count via Microsoft Graph API?


I'm using the Microsoft Graph API to retrieve the size and item count of the Deleted Items folder for a user's mailbox. Here's the request I'm using: GET https://graph.microsoft.com/v1.0/me/mailFolders/deleteditems

The API response returns the following:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('4011bbed-71be-429b-a234-1bec4112a689')/mailFolders/$entity",
    "id": "AQMkAGI1MDYxMzMAZS05MWE0LTQ2M.....3gmMwBACo1omjSnX9HglM9wY_BRqEAAAIBCgAAAA==",
    "displayName": "Deleted Items",
    "parentFolderId": "AQMkAGI1MDYxMzMAZS05M....mMwBACo1omjSnX9HglM9wY_BRqEAAAIBCAAAAA==",
    "childFolderCount": 0,
    "unreadItemCount": 0,
    "totalItemCount": 43,
    "sizeInBytes": 648887,
    "isHidden": false
}

However, when I check the Deleted Items folder in the user's mailbox (via Outlook or OWA), there are no emails in the folder. This discrepancy seems to be caused by the Recoverable Items folder, which is likely affecting the size and item count of the Deleted Items folder in the Graph API response.

Questions: Is there a way to exclude the impact of the Recoverable Items folder and retrieve only the actual size and count of emails in the Deleted Items folder using the Microsoft Graph API? If the Graph API cannot exclude the Recoverable Items folder, is there any recommended workaround to get accurate data (e.g., directly querying messages in Deleted Items or using filters)? Can the Recoverable Items folder be managed (queried or cleared) via the Microsoft Graph API?

Any guidance would be greatly appreciated. Thank you!


Solution

  • However, when I check the Deleted Items folder in the user's mailbox (via Outlook or OWA), there are no emails in the folder. This discrepancy seems to be caused by the Recoverable Items folder, which is likely affecting the size and item count of the Deleted Items folder in the Graph API response.

    Yes, The discrepancy between the sizeInbytes and totalItemCount is caused by the Recoverable Items folder being part of the Deleted Items metadata. The Deleted Items folder is affected by items that may have been moved to the Recoverable Items folder.

    As of now, Unfortunately, there is no direct way to exclude the Recoverable Items folder when retrieving the size and item count of the Deleted Items folder via the Graph API. So, you need to query them separately by querying deleted item and Recoverable items.

    Note: recoverableitemsdeletions contains soft-deleted items: deleted either from the Deleted Items folder, or by pressing shift+delete in Outlook. Refer this MsDoc.

    To list the deleted items from Delete Folder of mailbox:

    GET https://graph.microsoft.com/v1.0/me/mailFolders/deletedItems
    

    enter image description here

    To list the deleted items from Delete Folder with messages of mailbox:

    GET https://graph.microsoft.com/v1.0/me/mailFolders/deletedItems/messages?$filter=parentFolderId eq 'deletedItems'&$count=true
    

    enter image description here

    To list the RecoverableItemsDeletion folder of Mailbox:

    GET https://graph.microsoft.com/v1.0/me/mailFolders/recoverableitemsdeletions
    

    enter image description here

    To list the RecoverableItemsDeletion folder with messages of mailbox:

    enter image description here

    To query more endpoints of Recoverable Item Folder Refer this SO thread by Glen Scales

    Reference:

    SO thread by Glen Scales

    MsDocument on recoverableitemsdeletions