The Computer Oracle

Delete all files from a folder and its sub folders

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Life in a Drop

--

Chapters
00:00 Delete All Files From A Folder And Its Sub Folders
00:31 Answer 1 Score 23
00:43 Answer 2 Score 4
01:13 Accepted Answer Score 125
01:51 Answer 4 Score 5
01:57 Thank you

--

Full question
https://superuser.com/questions/741945/d...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows #script #powershell #batchfile #vbscript

#avk47



ACCEPTED ANSWER

Score 125


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




ANSWER 2

Score 23


You can do so with del command:

dir C:\folder
del /S *

The /S switch is to delete only files recursively.




ANSWER 3

Score 5


Using PowerShell:

Get-ChildItem -Path c:\temp -Include * | remove-Item -recurse 



ANSWER 4

Score 4


Reading between the lines on your original question I can offer an alternative BATCH code line you can use. What this will do when ran is only delete files that are over 60 days old. This way you can put this in a scheduled task and when it runs it deletes the excess files that you don't need rather than blowing away the whole directory. You can change 60 to 5 days or even 1 day if you wanted to. This does not delete folders.

forfiles -p "c:\path\to\files" -d -60 -c "cmd /c del /f /q @path"