Delete all files from a folder and its sub folders

362,222

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

Share:
362,222

Related videos on Youtube

BobJim
Author by

BobJim

Updated on September 18, 2022

Comments

  • BobJim
    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
      Ramhound about 10 years
      You 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 within Folder A
    • BobJim
      BobJim about 10 years
      I could do, I dont know how to script in powershell but have previous run powershell scripts.
    • MDMoore313
      MDMoore313 about 10 years
      Now is a perfect time to learn, I would research batch and vbs syntax for legacy purposes, but invest more time in learning PS.
    • BobJim
      BobJim about 10 years
      I 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
      Ben over 9 years
      I'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
      Kellen Stuart over 7 years
      If you want to delete recursively based on the extension: ls -Recurse *.docx | rm.
  • phoops
    phoops about 10 years
    Yes. You can add /P option so it will ask you to confirm every delete just to check that.
  • BobJim
    BobJim about 10 years
    thanks ill give it a go! hopefully will save loads of time!
  • Keltari
    Keltari about 10 years
    FYI - this will only delete files you have access to. This will not delete hidden or system files.
  • BobJim
    BobJim about 10 years
    Thanks 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
    MDMoore313 about 10 years
    It's -Include *.* not -Include ., big difference! :-)
  • BobJim
    BobJim about 10 years
    Get-ChildItem -Path D:\EmptyThisFolder -Include . -File -Recurse | foreach { $_.Delete()}
  • MDMoore313
    MDMoore313 about 10 years
    Why are you using -Include . ?? That's what the problem is.
  • Aylatan
    Aylatan over 8 years
    And you can use /Q to NOT ask for confirmation when using wildcards.
  • Malachi
    Malachi over 7 years
    Another 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
    MDMoore313 over 7 years
    @ThomasWeller lol it happens sometimes what version of powershell do you have?
  • MDMoore313
    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
    Thomas Weller over 7 years
    Can'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
    Kellen Stuart over 7 years
    can I ask, why | foreach { $_.Delete()} you could easily replace that expression with rm and it would be much easier to understand
  • MDMoore313
    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
    Anders Lindén over 6 years
    The title reads "Delete all files from a folder and its sub folders" so this is not a solution.
  • Anders Lindén
    Anders Lindén over 6 years
    The title reads "Delete all files from a folder and its sub folders" so this is not a solution.
  • demonicdaron
    demonicdaron over 5 years
    This does not delete subfolders in my case (also tried changing foreach { $_.Delete()} for rm)
  • demonicdaron
    demonicdaron over 5 years
    Nice and easy. This did the trick whilst the accepted answer did not!
  • MDMoore313
    MDMoore313 over 5 years
    @demonicdaron its not supposed to; The OP's question was for preserving the folder structure.
  • demonicdaron
    demonicdaron over 5 years
    @MDMoore313 sorry, should have read the OP better
  • J Garcia
    J Garcia over 4 years
    This 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
    A X over 4 years
    This doesn't support folders - the next answer does work though
  • MDMoore313
    MDMoore313 over 4 years
    @Abr it's not supposed to delete folders, it's supposed to answer the OP's question.
  • Nick Coad
    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
    Keith Miller about 4 years
    $Path = 'c:\Folder'; Get-ChildItem $Path -File -Recurse | Remove-Item
  • Dishant Batra
    Dishant Batra about 4 years
    Thanks @KeithMiller
  • cbailiss
    cbailiss over 3 years
    This 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
    laurencemadill about 3 years
    This doesn't meet the "so I'm left with an empty folder structure" part, it deletes folders too
  • NFR
    NFR about 3 years
    this will delete eveerything inside a folder (including sub-folders), but the OP wanted to keep all the sub-folders.
  • Nereis
    Nereis about 2 years
    Try "Remove-Item C:\Test\* -Include *.* -Recurse" which should do the trick but comes with limitations (see my answer below)