358

Using PowerShell, is it possible to remove some directory that contains files without prompting to confirm action?

Mwiza
  • 7,780
  • 3
  • 46
  • 42
hsz
  • 148,279
  • 62
  • 259
  • 315
  • 1
    Possible duplicate of [How to recursively delete an entire directory with PowerShell 2.0?](https://stackoverflow.com/questions/1752677/how-to-recursively-delete-an-entire-directory-with-powershell-2-0) – Michael Freidgeim Jul 11 '17 at 12:57

17 Answers17

523
Remove-Item -LiteralPath "foldertodelete" -Force -Recurse

or, with shorter version

rm /path -r -force
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
Michael Price
  • 8,088
  • 1
  • 17
  • 24
  • 25
    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 Dec 02 '11 at 20:02
  • 3
    If I want delete only contents of folder but not delete folder? – Kiquenet Mar 13 '13 at 08:26
  • 3
    @Kiquenet- This works for me, if I add a trailing slash to the path, so this example becomes Remove-Item .\foldertodelete\* -Force -Recurse – Adrian Carr Aug 19 '13 at 21:11
  • 4
    If you want it to ignore a missing folder you can add `-ErrorAction Ignore`, although that will also hide other errors. – Tor Klingberg May 13 '16 at 14:00
  • 3
    @Kiquenet Then you can use wildcards to remove everything within that folder: `Remove-Item './folder/*'`. If you really want to clear out only files of all folders you can list all leafs and pipe it to the Remove-Item cmdlet `Get-ChildItem -Recurse -File | Remove-Item` – Michael Kargl Jun 01 '19 at 11:32
  • 2
    To make the commands shorter ```rm /path -r -force``` – theshubhagrwl Feb 03 '21 at 12:25
  • rm /path -r -force doesn't work at all, it just spits out a ton of error messages about directories not being empty. – Andrew Koster Jan 09 '23 at 23:15
  • this doesn't work – WhyWhat May 28 '23 at 08:43
  • so close, i tried `rm -rf`, which didn't work, so the closest to linux is `rm -r -fo` – Spartan Jun 19 '23 at 17:53
84

From PowerShell remove force answer: help Remove-Item says:

The Recurse parameter in this cmdlet does not work properly

The command to workaround is

Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse

And then delete the folder itself

Remove-Item $Destination -Force 
Community
  • 1
  • 1
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
  • 1
    If I want delete only contents of folder but not delete folder? – Kiquenet Mar 13 '13 at 08:26
  • 3
    @beppe9000: I believe, yes. In the recent scripts I am using `Remove-Item -Recurse -Force $dir` and it works. – Michael Freidgeim Apr 04 '16 at 10:56
  • Ok, but I just read that the problem is still here on the windows 10 extended `Get-Help Remove-Item` documentation obtained after `Update-Help` is run... – beppe9000 Apr 04 '16 at 17:55
  • 2
    Get-ChildItem should also have the -Force argument, so that it also returns hidden files/folders. – Vlad Iliescu Apr 22 '16 at 09:20
  • @MichaelFreidgeim - I've used this and it has decreased intermittent failures, but they still occasionally happen. If Remove-Item -recurse is problematic for the top-level directory, can you explain why it would not be similarly problematic when it is piped into with the workaround line? Get-ChildItem -recurse doesn't return the children in a bottom-up order. Does Remove-Item order its pipelined input? – aggieNick02 May 03 '18 at 20:15
  • I think a potential failure mode with this is if $Destination is not specified, say while running a script that composes that value, the command deletes all the files in the directory in which you are working, which happened to me, but thanks be to git... – J E Carter II Feb 04 '21 at 22:08
  • 1
    I have modularized this answer into a function -- see [here](https://stackoverflow.com/a/37438430) – sam-6174 Jun 03 '21 at 18:28
  • Great if you want a shorter option : `rm -r -fo` – Samyar Apr 28 '23 at 10:42
67

This worked for me:

Remove-Item $folderPath -Force  -Recurse -ErrorAction SilentlyContinue

Thus the folder is removed with all files in there and it is not producing error if folder path doesn't exists.

necrifede
  • 781
  • 5
  • 8
44

2018 Update

In the current version of PowerShell (tested with v5.1 on Windows 10 and Windows 11 in 2023) 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.

One can also clean up a folder, but not the folder itself, using rm -R .\DirName\* (noted by Jeff in the comments).

divenex
  • 15,176
  • 9
  • 55
  • 55
  • 5
    none of the powershell commands nor this one works for me on a 2016 server core computer. They all say, `cannot be removed because it is not empty'. I also tried the rd command in windows. I can move the folder anywhere, just can't delete. – Post Impatica Mar 03 '20 at 14:01
25

in short, We can use rm -r -fo {folderName} to remove the folder recursively (remove all the files and folders inside) and force

Salman
  • 892
  • 12
  • 13
13

To delete content without a folder you can use the following:

Remove-Item "foldertodelete\*" -Force -Recurse
Eric Pohl
  • 2,324
  • 1
  • 21
  • 31
10

rm -Force -Recurse -Confirm:$false $directory2Delete didn't work in the PowerShell ISE, but it worked through the regular PowerShell CLI.

I hope this helps. It was driving me bannanas.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Flightdeck73
  • 303
  • 2
  • 8
  • Thank you, same goes for me! FInally this folder was deleted when calling from `PowerShell CLI` and not while developing in `PowerShell ISE`. – Bruno Bieri Aug 20 '18 at 11:58
8

This worked for me:

Remove-Item C:\folder_name -Force -Recurse
variable
  • 8,262
  • 9
  • 95
  • 215
6

Powershell works with relative folders. The Remove-Item has couple of useful aliases which aligns with unix. Some examples:

rm -R -Force ./directory
del -R -Force ./directory/*
4

Below is a copy-pasteable implementation of Michael Freidgeim's answer

function Delete-FolderAndContents {
    # http://stackoverflow.com/a/9012108

    param(
        [Parameter(Mandatory=$true, Position=1)] [string] $folder_path
    )

    process {
        $child_items = ([array] (Get-ChildItem -Path $folder_path -Recurse -Force))
        if ($child_items) {
            $null = $child_items | Remove-Item -Force -Recurse
        }
        $null = Remove-Item $folder_path -Force
    }
}
sam-6174
  • 3,104
  • 1
  • 33
  • 34
2
$LogPath = "E:\" # Your local of directories
$Folders = Get-Childitem $LogPath -dir -r | Where-Object {$_.name -like "*temp*"}
foreach ($Folder in $Folders) 
{
    $Item =  $Folder.FullName
    Write-Output $Item
    Remove-Item $Item -Force -Recurse
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
Anderson Braz
  • 371
  • 2
  • 4
2

Since my directory was in C:\users I had to run my powershell as administrator,

del ./[your Folder name] -Force -Recurse

this command worked for me.

OMKAR AGRAWAL
  • 128
  • 1
  • 9
1
$LogPath = "E:\" # Your local of directories
$Folders = Get-Childitem $LogPath -dir -r | Where-Object {$_.name -like "*grav*"} # Your keyword name directories

foreach ($Folder in $Folders) 
{
    $Item =  $Folder.FullName
    Write-Output $Item
    Remove-Item $Item -Force -Recurse -ErrorAction SilentlyContinue
}
Community
  • 1
  • 1
Anderson Braz
  • 371
  • 2
  • 4
1

If you have your folder as an object, let's say that you created it in the same script using next command:

$folder = New-Item -ItemType Directory -Force -Path "c:\tmp" -Name "myFolder"

Then you can just remove it like this in the same script

$folder.Delete($true)

$true - states for recursive removal

1

If you want to concatenate a variable with a fixed path and a string as the dynamic path into a whole path to remove the folder, you may need the following command:

$fixPath = "C:\Users\myUserName\Desktop"
Remove-Item ("$fixPath" + "\Folder\SubFolder") -Recurse

In the variable $newPath the concatenate path is now: "C:\Users\myUserName\Desktop\Folder\SubFolder"

So you can remove several directories from the starting point ("C:\Users\myUserName\Desktop"), which is already defined and fixed in the variable $fixPath.

$fixPath = "C:\Users\myUserName\Desktop"
Remove-Item ("$fixPath" + "\Folder\SubFolder") -Recurse
Remove-Item ("$fixPath" + "\Folder\SubFolder1") -Recurse
Remove-Item ("$fixPath" + "\Folder\SubFolder2") -Recurse
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
1

Some multi-level directory folders need to be deleted twice, which has troubled me for a long time. Here is my final code, it works for me, and cleans up nicely, hope it helps.

function ForceDelete {
    [CmdletBinding()]
    param(
        [string] $path
    )
    
    rm -r -fo $path
    
    if (Test-Path -Path $path){
        Start-Sleep -Seconds 1
        Write-Host "Force delete retrying..." -ForegroundColor white -BackgroundColor red
        rm -r -fo $path
    }
}

ForceDelete('.\your-folder-name')
ForceDelete('.\your-file-name.php')
  • this is just a wrapper around answers already posted. what benefit do you see in your Answer over the others? ///// also, calling a function with `()` around the values is not correct in PoSh ... that just says "treat this as an array". – Lee_Dailey Apr 16 '22 at 09:02
0

It looks cumbersome, but I use this to have everything reliably deleted, even if there are very long pathname\filenames (exceeding 259 characters):

  &mkdir    empty_dummy_directory              >$Null
  &robocopy empty_dummy_directory $folder /mir >$Null
  &rmdir    empty_dummy_directory
  &rmdir    $folder

Source of the solution and some more details and discussions are here.

Traveler
  • 213
  • 3
  • 10