# Script to clear cache and temporary files on Windows devices

Clearing all caches in Windows usually helps if your device is slowing down or exhibiting suboptimal performance. This usually happens when you’ve been on the internet for a while, installed Windows updates, or haven’t reset your computer in a long time.

For several reasons, clearing your cache regularly enhances the efficiency of your system. Depending on your settings, a cache can get rather large and take up a lot of disc space on your computer. The more data saved in the cache, the slower your computer will browse the web or do routine tasks. Delete the cache data to aid debugging, improve web page loading times, and boost your computer’s performance.

Using Hexnode, you can [deploy scripts](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) to effortlessly delete all temporary files and directories as well as empty the recycle bin.

 Note: 
**Supported Versions**:

The scripts given below will be supported on the following versions:

- Windows 10 v1607+ (Pro, Enterprise, Education)
- Windows 11 (Pro, Enterprise, Education)

 

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

PowerShell scripts
------------------

**1) Remove temporary files**

Script to remove temporary files on Windows

$objShell = New-Object -ComObject Shell.Application $objFolder = $objShell.Namespace(0xA) $WinTemp = "C:\\Windows\\Temp\\\*" #1# Remove Temp Files write-Host "Removing Temp" -ForegroundColor Green Set-Location "C:\\Windows\\Temp" Remove-Item \* -Recurse -Force -ErrorAction SilentlyContinue Set-Location "C:\\Windows\\Prefetch" Remove-Item \* -Recurse -Force -ErrorAction SilentlyContinue Set-Location "C:\\Documents and Settings" Remove-Item ".\\\*\\Local Settings\\temp\\\*" -Recurse -Force -ErrorAction SilentlyContinue Set-Location "C:\\Users" Remove-Item ".\\\*\\Appdata\\Local\\Temp\\\*" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item ".\\\*\\AppData\\Local\\Microsoft\\Windows\\INetCache\\\*" -Recurse -Force -ErrorAction SilentlyContinue #2# Running Disk Clean up Tool write-Host "Running the Windows Disk Clean up Tool" -ForegroundColor White cleanmgr /sagerun:1 | out-Null $(\[char\]7) Sleep 3 write-Host "Cleanup task complete!" -ForegroundColor Yellow Sleep 3 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

  $objShell = New-Object -ComObject Shell.Application

$objFolder = $objShell.Namespace(0xA)

$WinTemp = "C:\\Windows\\Temp\\\*"

\#1# Remove Temp Files 

write-Host "Removing Temp" -ForegroundColor Green 

Set-Location "C:\\Windows\\Temp"

Remove-Item \* -Recurse -Force -ErrorAction SilentlyContinue 

Set-Location "C:\\Windows\\Prefetch"

Remove-Item \* -Recurse -Force -ErrorAction SilentlyContinue 

Set-Location "C:\\Documents and Settings"

Remove-Item ".\\\*\\Local Settings\\temp\\\*" -Recurse -Force -ErrorAction SilentlyContinue 

Set-Location "C:\\Users"

Remove-Item ".\\\*\\Appdata\\Local\\Temp\\\*" -Recurse -Force -ErrorAction SilentlyContinue 

Remove-Item ".\\\*\\AppData\\Local\\Microsoft\\Windows\\INetCache\\\*" -Recurse -Force -ErrorAction SilentlyContinue

\#2# Running Disk Clean up Tool 

write-Host "Running the Windows Disk Clean up Tool" -ForegroundColor White 

cleanmgr /sagerun:1 | out-Null

$(\[char\]7)

Sleep 3

write-Host "Cleanup task complete!" -ForegroundColor Yellow 

Sleep 3 

   

 

  

**2) Clear Recycle Bin**

Script to clear recycle bin

$Path = 'C' + ':\\$Recycle.Bin' Get-ChildItem $Path -Force -Recurse -ErrorAction SilentlyContinue | Remove-Item -Recurse -exclude \*.ini -ErrorAction SilentlyContinue write-Output "Recycle Bin is empty."

   1

2

3

4

  $Path = 'C' + ':\\$Recycle.Bin'

Get-ChildItem $Path -Force -Recurse -ErrorAction SilentlyContinue |

Remove-Item -Recurse -exclude \*.ini -ErrorAction SilentlyContinue

write-Output "Recycle Bin is empty."

   

 

  

Batch script
------------

**1) Remove temporary files**

Batch script to clear cache and temporary files on Windows devices

@echo off echo is clearing system junk files, please wait... del /f /s /q %windir%\\prefetch\\\*.\* & rd /s /q %windir%\\temp & md %windir%\\temp echo clear system garbage is complete! 

   1

2

3

4

5

6

7

  @echo off 

echo is clearing system junk files, please wait...

del /f /s /q %windir%\\prefetch\\\*.\* & rd /s /q %windir%\\temp & md %windir%\\temp 

echo clear system garbage is complete! 

   

 

  

**2) Clear Recycle Bin**

Batch script to clear recycle bin on Windows devices

@echo off echo Emptying Recycle Bin.... rd /s /q C:\\$Recycle.Bin if %errorlevel% neq 0 ( echo Failed to remove C:\\$Recycle.Bin. Run as Administrator. exit /b 1 ) echo Done. Windows will recreate the Recycle Bin automatically. exit /b 0

   1

2

3

4

5

6

7

8

9

  @echo off

echo Emptying Recycle Bin....

rd /s /q C:\\$Recycle.Bin

if %errorlevel% neq 0 (

 echo Failed to remove C:\\$Recycle.Bin. Run as Administrator.

 exit /b 1

)

echo Done. Windows will recreate the Recycle Bin automatically.

exit /b 0

   

 

  

![Script to clear Recycle Bin on Windows remotely executed successfully from Hexnode.](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2022/03/Recycle-Bin-cleared-remotely-on-Windows-with-script--300x149.png)

How to View Script Output in Hexnode
------------------------------------

To review the execution results, navigate to the **Action History** tab of the specific device in your Hexnode UEM portal. Locate the script entry in the **Subject** column and click the **Show Output** button next to the status field to view the returned data.

 Notes:- Once the script is run successfully, restart your device for the changes to take effect.
- Windows do not allow any files or folders that are still in use to be deleted.
- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.