How to recursively delete an entire directory with PowerShell 2.0?

What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.

I have learned from several sources that the most obvious command, Remove-Item $targetDir -Recurse -Force, does not work correctly. This includes a statement in the PowerShell V2 online help (found using Get-Help Remove-Item -Examples) that states:

...Because the Recurse parameter in this cmdlet is faulty, the command uses the Get-Childitem cmdlet to get the desired files, and it uses the pipeline operator to pass them to the Remove-Item cmdlet...

I have seen various examples that use Get-ChildItem and pipe it to Remove-Item, but the examples usually remove some set of files based on a filter, not the entire directory.

I am looking for the cleanest way to blow out an entire directory, files and child directories, without generating any user warning messages using the least amount of code. A one-liner would be nice if it is easy to understand.

Remove-Item -Recurse -Force some_dir

does indeed work as advertised here.

rm -r -fo some_dir

are shorthand aliases that work too.

As far as I understood it, the -Recurse parameter just doesn't work correctly when you try deleting a filtered set of files recursively. For killing a single dir and everything below it seems to work fine.

How to quietly remove a directory with content in PowerShell, Possible duplicate of How to recursively delete an entire directory with PowerShell 2.0? From PowerShell remove force answer: help Remove-Item says: Thus the folder is removed with all files in there and it is not producing error if folder path doesn't exists. $true - states for recursive removal. How to recursively delete an entire directory with PowerShell 2.0? (10) What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.

I used:

rm -r folderToDelete

This works for me like a charm (I stole it from Ubuntu).

Remove-Item, This includes a statement in the PowerShell V2 online help (found using When deleting files recursively using a simple Remove-Item "folder" -Recurse I  In this example, we'll remove the folder D:\Temp\Test Folder1 recursively. In first example, PowerShell confirms if directory is not empty. In this case, it will simply delete the item. Type the following command in PowerShell ISE Console. Remove-Item 'D:\temp\Test Folder' -Recurse You can see the content of temp folder in Windows Explorer

When deleting files recursively using a simple Remove-Item "folder" -Recurse I sometimes see an intermittent error : [folder] cannot be removed because it is not empty.

This answer attempts to prevent that error by individually deleting the files.

function Get-Tree($Path,$Include='*') { 
    @(Get-Item $Path -Include $Include -Force) + 
        (Get-ChildItem $Path -Recurse -Include $Include -Force) | 
        sort pspath -Descending -unique
} 

function Remove-Tree($Path,$Include='*') { 
    Get-Tree $Path $Include | Remove-Item -force -recurse
} 

Remove-Tree some_dir

An important detail is the sorting of all the items with pspath -Descending so that the leaves are deleted before the roots. The sorting is done on the pspath parameter since that has more chance of working for providers other than the file system. The -Include parameter is just a convenience if you want to filter the items to delete.

It's split into two functions since I find it useful to see what I'm about to delete by running

Get-Tree some_dir | select fullname

Delete all files from a folder and its sub folders, This can be accomplished using PowerShell: Get-ChildItem -Path C:\Temp -​Include *.* -File -Recurse | foreach { $_.Delete()}. This command gets each child item  This is a short and simple PowerShell script to recursively delete empty folders from a folder structure. This can be necessary for a multitude of reasons - one of which you might be aware of since you're reading this. It uses the dreaded Write-Host quite a bit, because I wrote it in an early stage of learning PowerShell.

rm -r ./folder -Force    

...worked for me

Use PowerShell to Delete Files from the Command Line, Learn how to delete files and folders using PowerShell in this Ask an Admin. Find out how to remove all MP3 files, filter, remove .txt files and  I have seen various examples that use Get-ChildItem and pipe it to Remove-Item, but the examples usually remove some set of files based on a filter, not the entire directory. I am looking for the cleanest way to blow out an entire directory, files and child directories, without generating any user warning messages using the least amount of code.

Try this example. If the directory does not exist, no error is raised. You may need PowerShell v3.0.

remove-item -path "c:\Test Temp\Test Folder" -Force -Recurse -ErrorAction SilentlyContinue

How to recursively delete an entire directory with PowerShell 2.0?, What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerSh. In the current version of PowerShell (tested with v5.1 on Windows 10 1809) one can use the simpler Unix syntax rm -R .\DirName to silently delete the directory .\DirName with all subdirectories and files it may contain. In fact many common Unix commands work in the same way in PowerShell as in a Linux command line.

Delete all empty folders in a directory tree, SBS 2008 and PowerShell 2.0. I used the following script (running from within the PowerShell ISE as an administrator) to recursively delete only empty folders:. When attempting to remove a directory using a command, such as rmdir, you may receive a prompt similar to "rmdir: 'dir': Directory not empty" and be unable to delete the directory. To remove a directory that contains other files or directories, use the following command. rm -r mydir. In the example above, you would replace "mydir" with the name

Using PowerShell to Delete Files [All the Ways], In PowerShell 2.0, the first WMI cmdlets were released, which are not mentioned in this Example 2: Using PowerShell to Delete All Files in Folder (WMI) List all files in C:\Windows\Web\ recursively using Get-ChildItem  Today I want to talk about deleting directories, and I will show you three ways to delete folders. Unlike yesterday, I want to talk about what I consider the best way to delete a directory first. Method 1: Use native cmdlets. To delete folders, I like to use the Remove-Item cmdlet. There is an alias for the Remove-Item cmdlet called rd.

Powershell delete files only from directory, Try this: Get-ChildItem *.* -recurse | Where { ! $_.PSIsContainer }. Found it here:  Use PowerShell to Delete a Single File or Folder. Let’s start by running a simple command to delete a single file or folder. Make sure you are logged in to the server or PC with an account that

Comments
  • powershell, i know, but RD /S /Q
  • possible duplicate: stackoverflow.com/questions/1667145/…
  • I don't think it is a duplicate. I reviewed 1667145 before posting. It is asking why PowerShell is not setting the Recurse bool parameter properly when calling the Remove-Item method implemention of a custom PowerShell provider. I was asking about Remove-Item behavior as it relates to the the built in file system provider.
  • "RD /S /Q" doesn't seem to work in PowerShell -- says "Remove-Item : A positional parameter cannot be found that accepts argument '/q'."
  • rd is an alias for Remove-Item in powershell. cmd /c "rd /s /q" works, though.
  • I think you are correct. I was getting a "Cannot remove the item at 'some directory' because it is in use." error and assumed it was an issue with the recursion algorithm and went searching for a workaround. It turns out I had a process I fired off earlier in the script that was working in the target directory. When changed the script to wait for the other process the "Remove-Item -Recurse -Force" command works. Always look in the mirror first:)
  • I've found that I need to run this twice when run on a directory that contains subdirectories. The first time, there will be a lot of "The directory is not empty" errors. The second time, it completes with no errors.
  • Kristopher Johnson, I get similar errors with varying tools on Windows 7. It seems that the delete call returns earlier than a file or folder is actually removed, causing trouble sometimes. This seems to happen in Explorer, Far, cmd and PowerShell.
  • @Joey "It seems that the delete call returns earlier than a file or folder is actually removed, causing trouble sometimes." --> For clarification: the parent folder will not be deleted and one gets the following error: "[parent folder] cannot be removed because it is not empty." I see this happen constantly on (slow) networked drives. The only solution is the old one: cmd /c rd as stated below.
  • What's about "The directory is not empty" errors ?serverfault.com/questions/199921/powershell-remove-force Maybe better get-childitem * -include *.csv -recurse | remove-item I don't know. See stackoverflow.com/a/1668471/206730
  • Doesn't that require cygwin, git, or some other tool that can simulate a bash shell on Windows?
  • @Pete, no, it does not require anything but PowerShell. rm is an alias for Remove-Item in PowerShell's default configuration. Check the output of Get-Alias rm for more details. The -r is taking advantage of PowerShell's partial matching behavior on parameters. Since Remove-Item only has the one parameter that starts with an 'r', -Recurse, -r matches that. Thus, the following all will work the same: rm -r, rm -re, Remove-Item -Recurse. (Note that neither rm -rf nor rm -r -f will work, but rm -r -fo will. -rf matches no parameters and -f matches more than one.)