Delete Files in the Microsoft Teams Chat Files folder in OneDrive

Ozan Koyuncu 0 Reputation points
2025-06-13T13:53:15.3766667+00:00

Hello,

I am seeking technical advice and guidance on implementing an automated solution to delete files and folders in all Microsoft 365 users' Microsoft Teams Chat Files folder within their OneDrive directory.

I have attempted to achieve this using Power Automate and PowerShell scripts, but neither has worked as expected thus far. For context:

Power Automate:

I have an Enterprise App and a Service Principal in Azure, configured with the client ID, client secret, and client credential. The app is assigned the following permissions:

User.Read.All

Files.ReadWrite.All

These permissions are retrieved in Power Automate using an HTTP Connector with a POST method and the Graph API endpoint:
https://graph.microsoft.com/v1.0/users/drive/root/children

I then use a Parse JSON action to extract the Access Token, followed by another HTTP Connector with a GET method, including:

Authorization: "Bearer {access-token}"

Content-Type: "application/json"

At a high level, the issue is that a user must sign in to create the connection and link to OneDrive. As far as I’m aware, without users explicitly granting permissions to an admin/service account to access their OneDrive directory, I don’t see an alternative way to make this work - this would also require user interaction, which we want to avoid.

I raised this with Microsoft support and to summarise received this response: Microsoft does not allow access to a user's OneDrive personal files using application permissions, even for admins. Only delegated permissions are permitted for /me/drive or /users/{id}/drive. In other words, you cannot silently access or delete files from a user's OneDrive without their sign-in unless they’re interacting with the app. This is by design - Microsoft enforces this restriction to protect user data.

PowerShell:

Using Intune and PowerShell, I created a script that:

Determines the OneDrive path in the local user's profile.

Identifies the Teams Chat Files folder location.

If the folder exists, retrieves all files and folders within it.

Attempts to sync and download any online-only files (I discovered that cloud-based, online-only files remain during the deletion process).

Deletes the files and folders.

The script is packaged and deployed via Intune, which also creates a Task Scheduler entry on the local machine to run the PowerShell script.

I tested this on my own account and another user's, but the other user reported that during the sync process, the script negatively impacted device performance. Due to this issue, I abandoned this approach.

Has anyone else encountered this challenge or found alternative solutions? Any advice would be greatly appreciated.

Thanks,
Ozz

Microsoft Teams | Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Teddie-D 1,280 Reputation points Microsoft External Staff Moderator
    2025-06-13T15:05:05.25+00:00

    Hi @Ozan Koyuncu  

    Thank you for posting your question in the Microsoft Q&A forum. 

    Also, thanks for your detailed explanation and for sharing the steps you've taken so far. We truly appreciate the time and effort you've invested in exploring both Power Automate and PowerShell-based approaches. 

    After reviewing your scenario, we regret to inform you that the behavior you're encountering is by design. Microsoft does not currently support application-level access to users’ OneDrive for Business content without explicit user consent. This restriction applies even to administrators and is enforced to protect user privacy and data integrity.  

    We understand how limiting this can be, especially when trying to implement organization-wide automation. Unfortunately, we have not identified any supported workarounds that would allow silent or unattended deletion of these files across all users’ OneDrive directories. Many users have expressed similar concerns on our Ideas · Community and we encourage you to share your experience there as well. Your feedback is incredibly valuable and helps guide future improvements to our products and services.  

    We sincerely apologize that we are unable to offer a solution or workaround at this time. Please know that your concerns have been heard, and we will continue to advocate for improvements in this area. 

    Thank you for your understanding and continued support. 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".   

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Prasad-MSFT 8,981 Reputation points Microsoft External Staff Moderator
    2025-06-16T05:31:13.9566667+00:00

    Microsoft enforces strict security and privacy policies for accessing user data in OneDrive, including the Microsoft Teams Chat Files folder. As you've discovered, application permissions cannot be used to access or delete files in a user's OneDrive without their explicit consent. This restriction is by design to protect user data.

    Thanks, 

    Prasad Das

    ************************************************************************* 

    If the response is helpful, please click on "upvote" button. You can share your feedback via Microsoft Teams Developer Feedback link. Click here to escalate.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.