Delete all files from a folder and its sub folders
Solution 1
This can be accomplished using PowerShell:
Get-ChildItem -Path C:\Temp -Include *.* -File -Recurse | foreach { $_.Delete()}
This command gets each child item in $path
, executes the delete method on each one, and is quite fast. The folder structure is left intact.
If you may have files without an extension, use
Get-ChildItem -Path C:\Temp -Include * -File -Recurse | foreach { $_.Delete()}
instead.
It appears the -File
parameter may have been added after PowerShell v2. If that's the case, then
Get-ChildItem -Path C:\Temp -Include *.* -Recurse | foreach { $_.Delete()}
It should do the trick for files that have an extension.
If it does not work, check if you have an up-to-date version of Powershell
Solution 2
Short and sweet PowerShell. Not sure what the lowest version of PS it will work with.
Remove-Item c:\Tmp\* -Recurse -Force
Solution 3
You can do so with del
command:
dir C:\folder
del /S *
The /S
switch is to delete only files recursively.
Solution 4
Using PowerShell:
Get-ChildItem -Path c:\temp -Include * | remove-Item -recurse
Solution 5
Use PowerShell to Delete a Single File or Folder. Before executing the Delete command in powershell we need to make sure you are logged in to the server or PC with an account that has full access to the objects you want to delete.
With Example: http://dotnet-helpers.com/powershell-demo/how-to-delete-a-folder-or-file-using-powershell/
Using PowerShell commnads to delete a file
Remove-Item -Path "C:\dotnet-helpers\DummyfiletoDelete.txt"
The above command will execute and delete the “DummyfiletoDelete.txt” file which present inside the “C:\dotnet-helpers” location.
Using PowerShell commnads to delete all files
Remove-Item -Path "C:\dotnet-helpers*.*"
Using PowerShell commnads to delete all files and folders
Remove-Item -Path "C:\dotnet-helpers*.*" -recurse
-recurse
drills down and finds lots more files. The –recurse
parameter will allow PowerShell to remove any child items without asking for permission. Additionally, the –force parameter can be added to delete hidden or read-only files.
Using -Force command to delete files forcefully
Using PowerShell command to delete all files forcefully
Remove-Item -Path "C:\dotnet-helpers*.*" -Force
Related videos on Youtube
BobJim
Updated on September 18, 2022Comments
-
BobJim over 1 year
I want to remove all files from a folder structure, so I'm left with an empty folder structure.
Can this be achieved in either batch or VBScript scripting?
I have tried a very basic batch command, but this required the user to allow the deletion of each file. This wasn't a suitable solution as there are many hundreds of files and this will increase massively over time.
What can you suggest?
-
Ramhound about 10 yearsYou mean a recurssive delete? This can indeed be done. Something like
rd /s /q "c:\folder a
will perform a recursive delete on all files and folders withinFolder A
-
BobJim about 10 yearsI could do, I dont know how to script in powershell but have previous run powershell scripts.
-
MDMoore313 about 10 yearsNow is a perfect time to learn, I would research batch and vbs syntax for legacy purposes, but invest more time in learning PS.
-
BobJim about 10 yearsI havent had the chance to check the code yet although I'm sure it will work. I will come back and select the most appropiate answer to my initial query.
-
Ben over 9 yearsI'll second @BigHomie's recommendation - I just learned some PowerShell and I'd have to say it's pretty neat - not too hard, a good weapon in any coder's arsenal. I use it fairly frequently now.
-
Kellen Stuart over 7 yearsIf you want to delete recursively based on the extension:
ls -Recurse *.docx | rm
.
-
-
phoops about 10 yearsYes. You can add
/P
option so it will ask you to confirm every delete just to check that. -
BobJim about 10 yearsthanks ill give it a go! hopefully will save loads of time!
-
Keltari about 10 yearsFYI - this will only delete files you have access to. This will not delete hidden or system files.
-
BobJim about 10 yearsThanks for the idea but currently I think the "emptying" process will be completed on a very unscheduled adhoc time frame. I'll keep that code for the future though! Cheers
-
MDMoore313 about 10 yearsIt's
-Include *.*
not-Include .
, big difference! :-) -
BobJim about 10 yearsGet-ChildItem -Path D:\EmptyThisFolder -Include . -File -Recurse | foreach { $_.Delete()}
-
MDMoore313 about 10 yearsWhy are you using
-Include .
?? That's what the problem is. -
Aylatan over 8 yearsAnd you can use /Q to NOT ask for confirmation when using wildcards.
-
Malachi over 7 yearsAnother example.. say you want to remove .mov (video) files from your folders that contain images as well... then you would do this: del *.mov /S (from the root of the structure of course)
-
MDMoore313 over 7 years@ThomasWeller lol it happens sometimes what version of powershell do you have?
-
MDMoore313 over 7 years@ThomasWeller Powershell is awesome, why not just upgrade powershell? If you're running an out of the box version of windows 7 that isn't fully patched yet you can't blame M$ I mean it is what it is.
-
Thomas Weller over 7 yearsCan't they implement
powershell --version
? Can't they use a version number with positive digits? It's just crazy. More than 1300 upvotes for a question on how to find out a version number of a program? -
Kellen Stuart over 7 yearscan I ask, why
| foreach { $_.Delete()}
you could easily replace that expression withrm
and it would be much easier to understand -
MDMoore313 about 7 years@KolobCanyon I suppose you could do that, however it being much easier to understand is subjective. Not only that, but you get to see what powershell is doing when you actually type rm, it calls the delete method on each object.
-
Anders Lindén over 6 yearsThe title reads "Delete all files from a folder and its sub folders" so this is not a solution.
-
Anders Lindén over 6 yearsThe title reads "Delete all files from a folder and its sub folders" so this is not a solution.
-
demonicdaron over 5 yearsThis does not delete subfolders in my case (also tried changing foreach { $_.Delete()} for rm)
-
demonicdaron over 5 yearsNice and easy. This did the trick whilst the accepted answer did not!
-
MDMoore313 over 5 years@demonicdaron its not supposed to; The OP's question was for preserving the folder structure.
-
demonicdaron over 5 years@MDMoore313 sorry, should have read the OP better
-
J Garcia over 4 yearsThis should be the accepted answer, you can even just type
rm *
since it's not really advisable to force (the confirmation dialog has a "Yes to All" option anyway). -
A X over 4 yearsThis doesn't support folders - the next answer does work though
-
MDMoore313 over 4 years@Abr it's not supposed to delete folders, it's supposed to answer the OP's question.
-
Nick Coad over 4 years@JGarcia the OP said they didn't want user input at all, so -Force is needed in this case.
-
Keith Miller about 4 years
$Path = 'c:\Folder'; Get-ChildItem $Path -File -Recurse | Remove-Item
-
Dishant Batra about 4 yearsThanks @KeithMiller
-
cbailiss over 3 yearsThis is nice and simple but is not exactly what the OP asked for. He wanted to leave the folder structure in place (but just delete the files). This removes the files and folders so you are left with nothing.
-
laurencemadill about 3 yearsThis doesn't meet the "so I'm left with an empty folder structure" part, it deletes folders too
-
NFR about 3 yearsthis will delete eveerything inside a folder (including sub-folders), but the OP wanted to keep all the sub-folders.
-
Nereis about 2 yearsTry "Remove-Item C:\Test\* -Include *.* -Recurse" which should do the trick but comes with limitations (see my answer below)