hsz
hsz

Reputation: 152284

How to quietly remove a directory with content in PowerShell

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

Upvotes: 417

Views: 618398

Answers (17)

Traveler
Traveler

Reputation: 257

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.

Upvotes: -1

divenex
divenex

Reputation: 17246

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).

Upvotes: 50

SwissCodeMen
SwissCodeMen

Reputation: 4895

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

Upvotes: 1

Michael Price
Michael Price

Reputation: 9028

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

or, with shorter version

rm /path -r -force

Upvotes: 608

Aspirant Zhang
Aspirant Zhang

Reputation: 31

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')

Upvotes: 1

Imtiaz Shakil Siddique
Imtiaz Shakil Siddique

Reputation: 4328

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/*

Upvotes: 6

variable
variable

Reputation: 9724

This worked for me:

Remove-Item C:\folder_name -Force -Recurse

Upvotes: 9

Salman
Salman

Reputation: 1040

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

Upvotes: 27

Anderson Braz
Anderson Braz

Reputation: 401

$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
}

Upvotes: 1

Dmitriy Reznikov
Dmitriy Reznikov

Reputation: 127

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

Upvotes: 1

OMKAR AGRAWAL
OMKAR AGRAWAL

Reputation: 138

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.

Upvotes: 2

Anderson Braz
Anderson Braz

Reputation: 401

$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
}

Upvotes: 2

Dmitriy N. Laykom
Dmitriy N. Laykom

Reputation: 185

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

Remove-Item "foldertodelete\*" -Force -Recurse

Upvotes: 13

sam2426679
sam2426679

Reputation: 3857

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
    }
}

Upvotes: 4

necrifede
necrifede

Reputation: 810

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.

Upvotes: 70

Michael Freidgeim
Michael Freidgeim

Reputation: 28501

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 

Upvotes: 90

Flightdeck73
Flightdeck73

Reputation: 313

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.

Upvotes: 10

Related Questions