PowerShell Basics: How to Delete Microsoft Teams Cache for All Users
Published Jul 14 2020 12:01 AM 207K Views
Microsoft

Sometimes there is a need to delete Microsoft Teams cache to quicken the adoption of an in-band policy change or simply troubleshoot an issue. The challenge here is that the cache for Microsoft Teams is in multiple directories. This can be done manually but would result in a slow and tedious process. Again, we turn to PowerShell to automate this process and this time it's a one-liner that addresses this opportunity. 

 

Get-ChildItem "C:\Users\*\AppData\Roaming\Microsoft\Teams\*" -directory | Where name -in ('application cache','blob storage','databases','GPUcache','IndexedDB','Local Storage','tmp') | ForEach{Remove-Item $_.FullName -Recurse -Force -WhatIf}

 

The end of the one-liner calls a -WhatIf which can be removed to enforce deletion.

 

As always, please share your comments below on bettering the above script or any questions you may have.

 

33 Comments
Brass Contributor

Do you need both get-childitems?

Why not just

 

Get-ChildItem "C:\Users\*\AppData\Roaming\Microsoft\Teams" -Directory| xxxx

instead?

Microsoft

@Terry Wrennall the MS Teams cache resides in multiple directories hence the multiple get-childitems call.

Copper Contributor

As get-childitem piped to get-childitem does not work in our Infra, i changed the Script to take only the current user:

Get-ChildItem -Path "C:\Users\$env:UserName\AppData\Roaming\Microsoft\Teams" -Directory|Where{$_ -in ('Cache','databases','blob_storage','IndexedDB','')}|ForEach{Remove-Item $_.FullName -Recurse -Force}

So every user would be able to kill his own cache only.

 

Regards

Pawel

Microsoft

@pkwdr Thank you for the share.  Different installations would have different cache storage paths so it is possible the script would need to be modified as required.

Microsoft

@Terry Wrennall and @pkwdr I modified the script based on both your feedback to create a more baseline get-childitem allowing readers to customize as needed.  Thank you both again for sharing your ideas.

Brass Contributor

Does Teams require to be closed? 

Microsoft

@Jgq85 Yes it would be recommended that Teams should be closed when clearing the cache.

Copper Contributor

Hi Anthony, we are using teams on vdi. After 8 to 10 hrs teams take all the memory even 10 gb. There seems to be memory leak issue with teams. Does this release memory also. 

Thanks

Deleted
Not applicable

With a stop process in there (to close Teams), using the AppData Env Variable

Stop-Process -Name Teams; Get-ChildItem -Path $env:AppData\Microsoft\Teams -Directory|Where{$_ -in ('Cache','databases','blob_storage','IndexedDB','')}|ForEach{Remove-Item $_.FullName -Recurse -Force}

with some shorthands:

spps -Name Teams; gci $env:AppData\Microsoft\Teams -Directory | ? { $_ -in ('Cache','databases','blob_storage','IndexedDB','') } | % { Remove-Item $_.FullName -Recurse -Force }
Brass Contributor

So how you gonna close a user lol. What if they're on a call 

Microsoft

Thank you @Deleted for the share.

Microsoft

@Jgq85 have a look at @Deleted's script share.  It would also be good practice to share notification with your users as to when the clean-up would occur.

Microsoft

@ShahGdubai315 this unfortunately does not release memory.  Let me look and see if thier is a solution for that issue.

Copper Contributor

That would be gr8 help if we can release memory. Its hogging all memory. We have 10 gb ram for each virtual machine and after a day it slowly takes all the memory and not releasing back. We have 100 vm machines and we need to reboot machines, its driving us crazy.

Many thanks

Brass Contributor

How Microsoft Teams uses memory: https://docs.microsoft.com/en-us/microsoftteams/teams-memory-usage-perf  

 

Clearing cache doesn't really fix "memory" issues. I've actually seen memory usage go UP after clearing folders/files. 

 

It uses memory based on memory available.

Try running Teams on a VM that has 8 GB of memory, then on one with 16 GB of memory the on one with 32 GB of memory. I'm betting if you leave it running as the same user for the same amount of time, its memory usage will not be consistent due to hardware difference.  

 

What they should do is allow you to throttle its memory usage if it's causing the issue.  Or allow you to only be loading/showing past week of messages to hopefully reduce memory usage or lag. 

 

Copper Contributor

@Jgq85 before we had 8 gb of ram and it was taking all. Then we upgraded ram to 10 and went upto 12 gb and 16gb but still the same whatever memory we assign it is taking all memory available in vm. Why MS is not fixing this big memory leak issue which is there future product which will b replacing skype.

Thanks

Right now the only solution is to kill the task and start it again.

Microsoft

Thanks for sharing!

Bronze Contributor

@Jgq85 @ShahGdubai315  Wow, is it worse in a VM for some reason? On a PC used with other things, where it can't just go crazy without starving everything else, it does seem to restrain itself, with the exception being video conferences. Is that how you're using it in a VM? It's Electron, so only so much is possible in terms of constraint.

Copper Contributor

I've tried the same but the cache isn't cleared yet.

lingitexpress_0-1595320113232.png

 

C:\Users\username\AppData\Roaming\Microsoft\Teams 

The folder has a file size of 185 MB.  Any advice. @Anthony Bartolo 

 

Deleted
Not applicable

@lingitexpressthat's because it's using the WhatIf parameter, if you remove the WhatIf Paremeter it will remove the files

Microsoft

Thank you @Deleted for your response.  @lingitexpress remove -WhatIf to enforce deletion as detailed at the end of the above post.  The -WhatIf is in place as a safeguard should you not wish to delete but only report on what cache can be deleted.

Copper Contributor

This worked great - thanks Anthony!

Note though, there is a minor typo in the command. Where the command says "blob storage" it should say "blob_storage".

Previous:

Get-ChildItem "C:\Users\*\AppData\Roaming\Microsoft\Teams\*" -directory | Where name -in ('application cache','blob storage','databases','GPUcache','IndexedDB','Local Storage','tmp') | ForEach{Remove-Item $_.FullName -Recurse -Force -WhatIf}

Corrected:

Get-ChildItem "C:\Users\*\AppData\Roaming\Microsoft\Teams\*" -directory | Where name -in ('application cache','blob_storage','databases','GPUcache','IndexedDB','Local Storage','tmp') | ForEach{Remove-Item $_.FullName -Recurse -Force -WhatIf}

 

Copper Contributor

i am getting below error and cache are not getting deleted, i am using this PS on server 2012 R2 OS where MS Teams installed.

 

ms teams cache.jpg

Copper Contributor

Processes teams.exe and outlook.exe block some files in the cache. If you want to wipe all cache completely, try this

 

Get-CimInstance -Query "select * from Win32_Process where Name='outlook.exe' or Name='teams.exe'" -ErrorAction Ignore |
    Where-Object -FilterScript {$owner = $_ | Invoke-CimMethod -MethodName GetOwner; $owner.User -eq $env:UserName} |
    Invoke-CimMethod -MethodName Terminate > $null

Remove-Item -Path $HOME\AppData\Roaming\Microsoft\Teams\* -Recurse -Force -ErrorAction Continue

WARNING: The first command kills outlook and teams processes for the current user.

Copper Contributor

Hi all,

 

Has anyone come across the need to remove Teams from users Appdata entirely? For example, using the machine wide installer and PCs that are hotdesks if someone doesn't sign back into that PC for 6 months, Teams is out of date and has vulnerabilities. 

 

I have been messing about with PowerShell and versioninfo with a less than attribute, but can't get it to work. We only want to remove versions older than x.x.x.x and leave alone versions not affected by vulnerabilities (in turn, this would not affect the currently signed in users Teams folder in appdata).

Copper Contributor

Having issues getting this to work in our environment. I log in with my domain account which has local admin rights, yet this script doesn't delete anything from the user profiles.


Trying to find a way of getting this to work, we have laptops with small HDD's (120GB) with >70 user profiles and the teams cache is taking up a significant amount of precious space. 

Copper Contributor

@Anthony Bartolo Thank you for the script, I was wondering if it's possible to apply this to a test group users. 

Copper Contributor

Thanks for the powershell script, a lot more powerfull than my old clearCache.bat :)

Iron Contributor

This is great information. We have a requirement to clear Teams cache for all users in order for a Microsoft newly released feature to work. 

Thanks,

Iron Contributor
 
Hello, Based on this discussion and people's input, I have come up with the following code. The initial testing works fine on my laptop. I am planning to run this script to all machines in my company. Is there anything can cause this script fail to clear teams cache? Thanks in advance,
 

# stop Teams

Stop-Process -Name Teams;    

#stop Outlook

Stop-Process -Name Outlook;

#wait for 5 second until Teams and Outlook closed

Start-Sleep -Seconds 5

#clear teams cache

Remove-Item -Path $HOME\AppData\Roaming\Microsoft\Teams\* -Recurse -Force -ErrorAction Continue

Copper Contributor

Hi px091,

 

How did you run this script to all machines in your company?

 

Regards,

 

Brass Contributor

WARNING: The first command kills outlook and teams processes for the current user.

@dmitrygancho no your script does not do that, it stops outlook and teams for the person RUNNING the script, the current user would be the person that is logged into the pc

 

i.e. id you ran 

Enter-PSSession -computername yyy

 then ran the script 

$env:UserName

is the person running the script, not the current user of the pc 

Copper Contributor

Good day,

can you help me, I need to clear the cache of an email account that starts with Microsoft teams and is connected in sync with a Yealink vp59 annex or telephone, I need to clear its cache since there are several contacts that no longer exist and they were given low. I hope your prompt help

Co-Authors
Version history
Last update:
‎May 04 2021 07:19 AM
Updated by: