Blog Post

Educator Developer Blog
1 MIN READ

How to Delete Microsoft Teams Cache for All Users via PowerShell

AnthonyBartolo's avatar
Mar 09, 2023

For many in education, Microsoft Teams is the collaboration platform that allows students and educators to chat, share files, and collaborate on projects. Like many apps, Microsoft Teams uses cache to store temporary data, such as chat history and files. While the cache is useful for improving performance, it can also become cluttered over time and slow down the app. Deleting the cache can help improve Microsoft Teams performance by reducing the amount of temporary data stored which can also free up storage space. 

 

The Microsoft Teams cache can be deleted manually by deleting all the files and folders in the %appdata%\Microsoft\Teams directory for each user profile. This however can be tedious and can be automated using PowerShell via one line of script.

 

 

 

Get-ChildItem -Path "$env:USERPROFILE\AppData\Local\Microsoft\Teams" -Recurse | Remove-Item -Force -Recurse

 

 

 

Note: It's important to close the Microsoft Teams app on all devices and clear the cache for all users to ensure that the cache is completely cleared.

 

Please leave any feedback or questions about how to improve the script shared above in the comments section below.

 

How to Delete Microsoft Teams Cache for All Users via PowerShell

 

Updated Apr 04, 2023
Version 2.0

9 Comments

  • ruffninja's avatar
    ruffninja
    Copper Contributor

    i know it's a bit late. 

    but i came with a safer code to achieve this cleanup

     

    $folders = Get-ChildItem -Path "C:\Users" 
    foreach ($fld in $folders) {
    $fullpath = "$fld\AppData\Roaming\Microsoft\Teams"
    gci -Path $fullpath -Recurse -Force
    if (test-path "$fullpath\Service Worker\CacheStorage") {
    Remove-Item "$fullpath\Service Worker\CacheStorage\*" -recurse}
        } 

     

     

  • Mr_Rakes's avatar
    Mr_Rakes
    Copper Contributor

    According to Microsoft's own doc it should be %appdata%\Microsoft\Teams for the cache location.

    https://learn.microsoft.com/en-us/microsoftteams/troubleshoot/teams-administration/clear-teams-cache

    Get-ChildItem -Path "$env:appdata\Microsoft\Teams"

     

    The %USERPROFILE%\AppData\Local\Microsoft\Teams locations looks like the installation directory.

     

    If Get-ChildItem has -recurse does the Remove-Item need -recurse?

  • Borg1of1's avatar
    Borg1of1
    Copper Contributor

    I reviewed both paths identified by the author.  %appdata%\Micorosft\Teams when put in the Run dialog resolves to C:\Users\<username>\AppData\Roaming\Microsoft\Teams while the PS command $env:USERPROFILE\AppData\Local\Microsoft\Teams is a completely different location.  Which is the correct path?  I am in favor of the AppData\Roaming.  

  • andydavid1's avatar
    andydavid1
    Brass Contributor

    Note you will probably need to close Outlook as well for this command to clear all the cached files.

  • TimLB, thank you for sharing this cautionary warning. It is important to be careful with powerful one-liners like this. It's crucial to understand the parameters and functions involved and to double-check before executing any commands that have the potential to wipe out large amounts of content.

     

    Your explanation of how the Recurse parameter works clarifies the potential danger of this command. I agree that it's essential to exercise caution and ensure that you have a backup plan in place before running any scripts that could potentially cause data loss. One could also add -WhatIf to the one-liner to detail what would be removed. 

     

    Thanks again for sharing.

  • Something like this should be built into Team's itself, triggered manually by the user and forces Teams to be reopened. To go further, just build it into the update process since that force closes Teams and reopens already.

  • TimLB's avatar
    TimLB
    Iron Contributor

    My system admin senses are tingling...

     

    Be careful with this command, it's a great and powerful one-liner but it has the potential to wipe out a LOT of content if it is used the wrong way. The "-Recurse" parameters are one part that will make this dangerous. Recurse (recursion) is a function that will essentially traverse the entire folder and file structure of a path when used like in the script above. So, the first part is getting a list of all the items within the local user's profile where Teams is storing all its cached files. That list of files is being fed in into the next command Remove-Item which also has a Recurse parameter applied. So put it all together and you have a command that will delete the entire contents of a single folder. Also, the files don't go into the Recycle Bin... so recovery may not be an option.

     

    There are very few technology professionals who do not have a story about themselves or someone else who has done a recursive delete that went the wrong direction.