Software-OK
≡... News | ... Home | ... FAQ | Impressum | Contact | Listed at | Thank you |

  
HOME ► Faq ► FAQ - Batch-Scripts ► ««« »»»

Delete temporary files with batch script!?


The script is useful, but there are some differences between Windows 10, 11, 12 and MS Server when deleting temporary files and the Recycle Bin:



IN ADVANCE:


 1. Include system-wide temp folder: `%temp%` only deletes user-specific files. The system-wide temp folder (`C:\Windows\Temp`) could also be included.

2. Empty the recycle bin for all users: Instead of `rd /s /q C:\$Recycle.Bin`, it is better to use `PowerShell` to empty the recycle bin for all users.

3. Check admin rights: Some commands require elevated rights. A query or direct elevation can be helpful.




1. Improved version of the script:
2. Differences between Windows 10, 11, 12 and MS Server
3. Summary of differences:






1.) Improved version of the script:



@echo off
:: Checks if the script is running as administrator
net session >nul 2>& 1 || ( echo Please run as administrator! & pause & exit )

echo Deleting temporary files ...
del /s /q "%temp% \*.* " >nul 2>&1
rd /s /q "%temp%" >nul 2>&1
md "%temp%" >nul 2>& 1

echo Deleting system-wide temporary files ...
del /s /q "C: \ Windows \ Temp \*.* " >nul 2>&1
rd /s /q "C: \ Windows \ Temp" >nul 2>&1
md "C: \ Windows \ Temp" >nul 2>&1

echo Empty the Recycle Bin ...
PowerShell -Command "Clear-RecycleBin -Force"

echo Cleanup completed!
pause
This version:

- Checks admin rights

- Also deletes system-wide temp files

- Empty the recycle bin with PowerShell




2.) Differences between Windows 10, 11, 12 and MS Server



when deleting temporary files and the recycle bin:



1. Temporary files (%temp% & C:\Windows\Temp)



✅ Windows 10, 11, 12 & Server

- `%temp%` is unique to each user.

- `C:\Windows\Temp` is used by system processes and requires administrator rights to delete.

- In Windows Server, some files in `C:\Windows\Temp` may be used by running services, so not all of them can be deleted.

2. Recycle Bin (`C:\$Recycle.Bin`)



🔹 Windows 10, 11, 12:

- `C:\$Recycle.Bin` contains all users' recycle bins.

- In newer versions of Windows, `Clear-RecycleBin` via PowerShell is more reliable.

🔹 Windows Server:

- Server administrators often disable the recycle bin, so emptying it may not be necessary.

- The command `PowerShell -Command "Clear-RecycleBin -Force"` works in Windows Server 2012 R2 and later.

3. Differences in the cleanup



💡 Windows 11 & 12:

- Use "Storage Sense", which automatically deletes temp files.

- Some temp files are locked by the system and cannot be removed with `del`.

💡 Windows Server:

- `C:\Windows\Temp` may contain sensitive logs or temporary installation files that may not be deleted.

- Some cleanup tools (like `Disk Cleanup`) are not installed by default on servers.

Recommended customization for Windows Server



If the script is running on Windows Server , it would be better to just delete `%temp%` first and not empty the Recycle Bin automatically. Instead, you could ask for confirmation:

@echo off
net session >nul 2>& 1 || ( echo Please run as administrator! & pause & exit )

echo Delete user temp files ...
del /s /q "%temp% \*.* " >nul 2>&1
rd /s /q "%temp%" >nul 2>&1
md "%temp%" >nul 2>& 1

echo Deleting system-wide temp files ...
del /s /q "C: \ Windows \ Temp \*.* " >nul 2>&1
rd /s /q "C: \ Windows \ Temp" >nul 2>&1
md "C: \ Windows \ Temp" >nul 2>&1

:: Ask for trash cleanup on server version
ver | findstr /i "server" >nul
if %errorlevel% equ 0 (
echo Do you want to empty the recycle bin ? ( Y/N )
set /p choice =
if /I "%choice%" == "Y" PowerShell -Command "Clear-RecycleBin -Force"
) else (
echo Empty the recycle bin ...
PowerShell -Command "Clear-RecycleBin -Force"
)

echo Cleanup completed!
pause




3.) Summary of the differences:



FeatureWindows 10 & 11Windows 12Windows Server
%temp%-Deletion ✅ Yes ✅ Yes ✅ Yes
C:\Windows\Temp ✅ Me (Admin) ✅ Me (Admin) ⚠️ Yes, but carefully
Empty the trash ✅ Yes (PowerShell) ✅ Yes (PowerShell) 🔹 Optional, often deactivated
Automatic cleanup ❌ Manual ✅ Memory optimization ❌ Manual







FAQ 4: Updated on: 27 February 2025 09:25 Windows
Batch-Scripts

Batch script to clear icon/thumbnail cache!?


To clear the icon cache and thumbnail cache in Windows 10, 11 and 12, you can use the following batch script: 1. Clear icon and thumbnail cache in Windows?
Batch-Scripts

What are Windows Batch Scripts!


Windows batch scripts, correctly called batch files, are simple text files that contain a series of commands that can be executed in the command line environment
Batch-Scripts

Windows batch file to establish a simple FTP connection?


To create a simple FTP connection using a Windows 12, 11, or 10 batch file and run some basic FTP commands, you can create a batch file Which executes the
Batch-Scripts

Batch script to clear icon/thumbnail cache!?


To clear the icon cache and thumbnail cache in Windows 10, 11 and 12, you can use the following batch script: 1. Clear icon and thumbnail cache in Windows?
Batch-Scripts

Delete temporary files with batch script!?


The script is useful, but there are some differences between Windows 10, 11, 12 and MS Server when deleting temporary files and the Recycle Bin: IN ADVANCE:

»»

  My question is not there in the FAQ
Asked questions on this answer:
Keywords: batch, scripts, delete, temporary, files, with, script, useful, there, some, differences, between, windows, server, when, deleting, recycle, advance, Questions, Answers, Software




  

  + Freeware
  + Order on the PC
  + File management
  + Automation
  + Office Tools
  + PC testing tools
  + Decoration and fun
  + Desktop-Clocks
  + Security

  + SoftwareOK Pages
  + Micro Staff
  + Freeware-1
  + Freeware-2
  + Freeware-3
  + FAQ
  + Downloads

  + Top
  + Desktop-OK
  + The Quad Explorer
  + Don't Sleep
  + Win-Scan-2-PDF
  + Quick-Text-Past
  + Print Folder Tree
  + Find Same Images
  + Experience-Index-OK
  + Font-View-OK


  + Freeware
  + BlankAndSecure
  + AlwaysMouseWheel
  + NewFileTime
  + DesktopClock3D
  + PointerStick
  + DesktopImages3D
  + WinPing
  + GetPixelColor
  + StressMyPC
  + PAD-s


Home | Thanks | Contact | Link me | FAQ | Settings | Windows 10 | gc24b | English-AV | Impressum | Translate | PayPal | PAD-s

 © 2025 by Nenad Hrg softwareok.de • softwareok.com • softwareok.com • softwareok.eu
32.047 msec.


► After Windows 10/11 Auto Update problems with FireFox browser, why? ◄
► Program with which you can transform JPG into PDF! ◄
► Share printers and add to sharing under Windows (11, 10, ...)! ◄
► Change the Windows folder icons RGB color! ◄


This website does not store personal data. However, third-party providers are used to display ads,
which are managed by Google and comply with the IAB Transparency and Consent Framework (IAB-TCF).
The CMP ID is 300 and can be individually customized at the bottom of the page.
more Infos & Privacy Policy

....