115

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?

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
RobN
  • 1,582
  • 5
  • 15
  • 27
  • 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` – Ramhound Apr 15 '14 at 12:47
  • I could do, I dont know how to script in powershell but have previous run powershell scripts. – RobN Apr 15 '14 at 13:41
  • 2
    Now is a perfect time to learn, I would research batch and vbs syntax for legacy purposes, but invest more time in learning PS. – MDMoore313 Apr 15 '14 at 14:26
  • 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. – RobN Apr 17 '14 at 07:08
  • 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. – Ben Dec 19 '14 at 17:13
  • If you want to delete **recursively** based on the extension: `ls -Recurse *.docx | rm`. – Kellen Stuart Oct 12 '16 at 20:34

16 Answers16

122

Short and sweet PowerShell. Not sure what the lowest version of PS it will work with.

Remove-Item c:\Tmp\* -Recurse -Force
tobassist
  • 128
  • 6
Evan Nadeau
  • 1,353
  • 1
  • 7
  • 2
  • 4
    Nice and easy. This did the trick whilst the accepted answer did not! – Kappacake Nov 28 '18 at 13:58
  • 2
    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). – J Garcia Oct 30 '19 at 04:49
  • @JGarcia the OP said they didn't want user input at all, so -Force is needed in this case. – Nick Coad Jan 01 '20 at 22:28
  • 5
    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. – cbailiss Sep 27 '20 at 07:59
  • 11
    This doesn't meet the "so I'm left with an empty folder structure" part, it deletes folders too – laurencemadill Feb 03 '21 at 10:54
  • 5
    this will delete eveerything inside a folder (including sub-folders), but the OP wanted to keep all the sub-folders. – NFR Mar 16 '21 at 04:46
  • Try "Remove-Item C:\Test\\* -Include \*.\* -Recurse" which should do the trick but comes with limitations (see my answer below) – Nereis Feb 17 '22 at 10:54
  • `Remove-Item` doc suggests not to use `-Recurse` as it has some issues. `https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-7.2#example-4-delete-files-in-subfolders-recursively` – Sahil Singh Mar 02 '23 at 05:26
118

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

MDMoore313
  • 5,956
  • 1
  • 27
  • 31
  • 1
    It's `-Include *.*` not `-Include .`, big difference! :-) – MDMoore313 Apr 25 '14 at 11:00
  • Get-ChildItem -Path D:\EmptyThisFolder -Include *.* -File -Recurse | foreach { $_.Delete()} – RobN Apr 25 '14 at 13:07
  • Why are you using `-Include .` ?? That's what the problem is. – MDMoore313 Apr 25 '14 at 13:08
  • @ThomasWeller lol it happens sometimes what version of powershell do you have? – MDMoore313 Oct 06 '16 at 18:45
  • @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. – MDMoore313 Oct 06 '16 at 18:50
  • 2
    Can't they implement `powershell --version`? Can't they use a version number with positive digits? It's just crazy. [More than 1300 upvotes](http://stackoverflow.com/questions/1825585/determine-installed-powershell-version) for a question on how to find out a version number of a program? – Thomas Weller Oct 06 '16 at 18:53
  • can I ask, why `| foreach { $_.Delete()}` you could easily replace that expression with `rm` and it would be **much** easier to understand – Kellen Stuart Oct 12 '16 at 20:41
  • @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. – MDMoore313 Mar 15 '17 at 16:43
  • This does not delete subfolders in my case (also tried changing foreach { $_.Delete()} for rm) – Kappacake Nov 28 '18 at 13:56
  • 1
    @demonicdaron its not supposed to; The OP's question was for _preserving_ the folder structure. – MDMoore313 Nov 29 '18 at 04:34
  • @MDMoore313 sorry, should have read the OP better – Kappacake Nov 29 '18 at 08:25
  • This doesn't support folders - the next answer does work though – A X Dec 22 '19 at 23:50
  • @Abr it's not supposed to delete folders, it's supposed to answer the OP's question. – MDMoore313 Dec 23 '19 at 01:12
23

You can do so with del command:

dir C:\folder
del /S *

The /S switch is to delete only files recursively.

ek9
  • 3,385
  • 3
  • 20
  • 20
  • 2
    Will this only delete files but leave the folders? – RobN Apr 15 '14 at 12:56
  • 5
    Yes. You can add `/P` option so it will ask you to confirm every delete just to check that. – ek9 Apr 15 '14 at 12:59
  • thanks ill give it a go! hopefully will save loads of time! – RobN Apr 15 '14 at 13:11
  • FYI - this will only delete files you have access to. This will not delete hidden or system files. – Keltari Apr 15 '14 at 13:33
  • 3
    Yes. And for deleting hidden and system files, do `attrib -s -r -h Folder\*.* /s /d` before deleting. And for permissions use `takeown` command. – Jet Apr 25 '14 at 13:49
  • 3
    And you can use /Q to **NOT** ask for confirmation when using wildcards. – Aylatan Sep 25 '15 at 00:41
  • 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) – Malachi Sep 23 '16 at 14:38
  • The title reads "Delete all files from a folder and its sub folders" so this is not a solution. – Anders Lindén Aug 22 '17 at 06:25
5

Using PowerShell:

Get-ChildItem -Path c:\temp -Include * | remove-Item -recurse 
bwDraco
  • 45,747
  • 43
  • 165
  • 205
4

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

Adil H. Raza
  • 103
  • 4
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"
Travis
  • 1,054
  • 8
  • 16
  • 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 – RobN Apr 15 '14 at 13:42
  • The title reads "Delete all files from a folder and its sub folders" so this is not a solution. – Anders Lindén Aug 22 '17 at 06:26
2

This is the easiest way IMO

Open PowerShell, navigate to the directory (cd), THEN

ls -Recurse * | rm

(Poof) everything is gone...

If you want to delete based on a specific extension

ls -Recurse *.docx | rm

ls is listing the directory

-Recurse is a flag telling powershell to go into any sub directories

* says everything

*.doc everything with .doc extension

| feed the output from the left to the right

rm delete

All the other answers appear to make this more confusing than necessary.

Kellen Stuart
  • 558
  • 3
  • 8
  • 18
1

We can delete all the files in the folder and its sub folders via the below command.

Get-ChildItem -Recurse -Path 'D:\Powershell Practice' |Where-Object{$_.GetType() -eq [System.IO.FileInfo]} |Remove-Item

Or

Get-ChildItem -Recurse -Path 'D:\Powershell Practice' -File | Remove-Item
1

Try this using PowerShell. In this example I want to delete all the .class files:

Get-ChildItem '.\FOLDERNAME' -include *.class -recurse | foreach ($_) {remove-item $_.FullName}
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Bacara
  • 111
  • 2
1
  1. In Windows Explorer select the root dir containing all the files and folders.

  2. Search for *

  3. Sort by Type (All the folders will be at the top and all the files listed underneath)

  4. Select all the files and press Delete.

This will delete all the files and preserve the directory structure.

Emel
  • 11
  • 1
1

Delete all files from current directory and sub-directories but leaving the folders structure.

(/Q) switch is for asking the user if he is ok to delete

Caution : try it without the /Q to make sure you are not deleting anything precious.

del /S * /Q 
hdoghmen
  • 113
  • 5
0
dir C:\testx\ -Recurse -File | rd -WhatIf
What if: Performing the operation "Remove File" on target "C:\testx\x.txt".
What if: Performing the operation "Remove File" on target "C:\testx\bla\x.txt".
bertieb
  • 7,344
  • 36
  • 42
  • 54
0

With Powershell 5.1:


$extensions_list = Get-ChildItem -Path 'C:\folder_path\' -Recurse

foreach ( $extension in $extensions_list) {

    if ($extension.Attributes -notlike "Directory") {

        Remove-Item $extension.FullName  
    }
}

It's removes all itens that are not Directory.

$extension.FullName = Item Path

$extension.Attributes = Item Type ( Directory or Archive )

0

PowerShell example.

I found some paths don't play nicely, so using -LiteralPath works in all cases.

Help on which can be found on the MS docs for Remove-Item.

# To delete all files within a folder and its subfolders.

$subDir = "Z:\a path to\somewhere\"

# Remove the -WhatIf and -Verbose here once you're happy with the result.
Get-ChildItem -LiteralPath $subDir -File -Recurse | Remove-Item -Verbose -WhatIf
Ste
  • 1,166
  • 2
  • 9
  • 23
0

If all your files have an extension and none of your folders have a dot, you can do the following:

Remove-Item C:\Test\* -Include *.* -Recurse

Check the official documentation which provides multiples examples.

Nereis
  • 101
  • 2
  • `Remove-Item` doc suggests not to use `-Recurse` as it has some issues. `https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-7.2#example-4-delete-files-in-subfolders-recursively` – Sahil Singh Mar 02 '23 at 05:26
  • Get-ChildItem may be more advisable but I haven't test but I'm not sure to see where the doc indicate issues. In any case, the above line worked like a charm in my case. – Nereis Mar 02 '23 at 10:38
  • This is from the doc - `Because the Recurse parameter in Remove-Item has a known issue, the command in this example uses Get-ChildItem to get the desired files, and then uses the pipeline operator to pass them to Remove-Item` – Sahil Singh Mar 03 '23 at 15:57
-1

As a complement to the above answers, actually there's no need to use the Get-Childitem and pass the result to the pipeline in the above answers, because the -Include keyword is included in the Remove-Item command

One can simply:

Remove-Item -Include "." "C:\Temp" -Recurse

Sams0nT
  • 1
  • 1