Share this article

Latest news

With KB5043178 to Release Preview Channel, Microsoft advises Windows 11 users to plug in when the battery is low

Copilot in Outlook will generate personalized themes for you to customize the app

Microsoft will raise the price of its 365 Suite to include AI capabilities

Death Stranding Director’s Cut is now Xbox X|S at a huge discount

Outlook will let users create custom account icons so they can tell their accounts apart easier

You can mass delete files/directories with CMD or Powershell, but there are faster options

Some of these solutions might be risky, though.

4 min. read

Published onFebruary 21, 2024

published onFebruary 21, 2024

Share this article

Read our disclosure page to find out how can you help Windows Report sustain the editorial teamRead more

In Windows, there are ways to mass delete files/directories using all kinds of third-party software, like the ones we tested in our2024 list.

However, the operating system is versatile enough to let users mass delete files and directories using CMD commands or Powershell commands to do so. For instance,this Reddit userknows this, and they developed two commands, one for CMD, and one for Powershell, that do the job.

If you want to try them, here they are.

The CMD command is this:

del /f /s /q “%USERPROFILE%\AppData\Local\Temp*.*” >nul 2>&1

for /d %%x in (“%USERPROFILE%\AppData\Local\Temp*.*”) do @rd /s /q “%%x”

The Powershell command is this:

Get-ChildItem -Path “$env:USERPROFILE\AppData\Local\Temp” -Force | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

However, the user wondered if there are faster options, as these commands don’t do the job fast enough. Needless to say, other users had a lot to say, and we gathered up some of the most popular responses. But if you’re curious about all of them, take a look at the Reddit thread.

The fastest way by far to mass delete files/folders is robocopy, which can be run using cmd.exe (may launch a second or two faster, execution time will be the same):

mkdir “%USERPROFILE%\empty” robocopy “%USERPROFILE%\empty” “%USERPROFILE%\AppData\Local\Temp” /mir rmdir “%USERPROFILE%\empty”

Or run using powershell.exe or pwsh.exe although it does not sound like PowerShell is needed here:

mkdir “$ENV:USERPROFILE\empty” & robocopy “$ENV:USERPROFILE\empty” “$ENV:USERPROFILE\AppData\Local\Temp” /mir rmdir “$ENV:USERPROFILE\empty”

Now, you might run into issues here due to files being in use

However, someone quickly added that using robocopy can be risky:

Be very careful with robocopy using /mir. Witnessed my partner in crime mirroring a large production file server one day to its new home on a schedule that had been running for a couple of weeks. He cut over to the new home over and deleted the source. Robocopy deleted all of production instantly. He turned white right before my eyes as he realised what had happened. Thank God we had shadow copies running. Fastest major recovery I have ever witnessed. Within 5 (or less) minutes all production was back. We never spoke of this again. But I am super careful with robocopy and the /mir switch.

Others were also successful in mass deleting files on Windows using dotnet:

I looked at deleting some huge folders (12Gb, 75k files, 7k folders) from a script a couple of years ago and settled on this dotnet call.

[System.IO.Directory]::Delete( $Folder.Fullname, $true )

It’s fast on a remote machine since it doesn’t enumerate locally. I’m not sure how it fits your use case. It does delete the base folder. You might be able to recreate it, or it might autocreate when used. Also, temp folders can have open files, not sure what happens there.

Others had other ideas:

Without putting it all in a scriptblock forMeasure-Command, I do not know.

I do know the directory you give is

$env:TEMPor%TEMP%99% of the time. It is located in%LOCALAPPDATA%or$env:LOCALAPPDATA.

I also know you don’t need to get-childitem. You could just

Remove-Item “$($env:TEMP)*” -Recurse -Forceordel /f /s /q %TEMP%

Also, /s takes care of all the subdirectories so you don’t need but the one command to do the same thing as bot commands and the loop.

And, ultimately, someone also used this:

Script C would be faster

Cd /d “%USERPROFILE%\AppData\Local\Temp\” rd /s /q .

This gets rid of the extra enumerations that the for loop you have in the command prompt version. It will throw an error at the end because you cannot delete the folder you are in. Command prompt will always be faster than powershell cmdlets because you have to build a file object as there is no native cmdlet that just builds a file path as a string. It takes time getting the added info like file size, creation date, etc. and creating said object. You will definitely see a difference if the number of files are in the five digits or higher. I use to maintain folders and files that needed deletion/archiving on a daily basis that were in the high 6 low 7 digits and this was usually the deletion solution that was fastest.

Once again, make sure to check the Reddit thread for other answers, as the post was made only hours ago, so there is a good chance more users will contribute to it.

More about the topics:file management tools

Flavius Floare

Tech Journalist

Flavius is a writer and a media content producer with a particular interest in technology, gaming, media, film and storytelling.

He’s always curious and ready to take on everything new in the tech world, covering Microsoft’s products on a daily basis. The passion for gaming and hardware feeds his journalistic approach, making him a great researcher and news writer that’s always ready to bring you the bleeding edge!

User forum

1 messages

Sort by:LatestOldestMost Votes

Comment*

Name*

Email*

Commenting as.Not you?

Save information for future comments

Comment

Δ

Flavius Floare

Tech Journalist

Flavius is a writer and a media content producer with a particular interest in technology, gaming, media, film and storytelling.