66
\$\begingroup\$

In my PowerShell script I'm trying to delete a folder, but only if it exists:

if (Test-Path $folder) { Remove-Item $folder -Recurse; }

I find myself repeating this combination of cmdlets quite a few times, and wished I had something like this:

# Pseudo code:
Remove-Item $folder -Recurse -IgnoreNonExistentPaths

Is there a way to do something like that? A different command, or an option I've missed from the Remove-Item documentation perhaps? Or is the only way to DRY out this bit of code to write my own cmdlet that combines Test-Path and Remove-Item?

RubberDuck
31.1k6 gold badges73 silver badges176 bronze badges
asked May 11, 2015 at 9:37
\$\endgroup\$
2
  • \$\begingroup\$ You may be looking for alias. \$\endgroup\$ Commented May 11, 2015 at 9:55
  • 2
    \$\begingroup\$ @Mast, an alias is just an alternative name for a command. You can't specify parameters or anything in an alias definition. \$\endgroup\$ Commented May 12, 2015 at 0:53

3 Answers 3

90
\$\begingroup\$

I assume you're just trying to avoid the error message in case it doesn't exist.

What if you just ignore it:

Remove-Item $folder -Recurse -ErrorAction Ignore

If that's not what you want, I would recommend writing your own function:

function Remove-ItemSafely {
[CmdletBinding(SupportsShouldProcess=$true)]
param(
 [Parameter(
 Mandatory=$true,
 ValueFromPipeline=$true,
 ValueFromPipelineByPropertyName=$true
 )]
 [String[]]
 $Path ,
 [Switch]
 $Recurse
)
 Process {
 foreach($p in $Path) {
 if(Test-Path $p) {
 Remove-Item $p -Recurse:$Recurse -WhatIf:$WhatIfPreference
 }
 }
 }
}
answered May 11, 2015 at 15:41
\$\endgroup\$
1
  • 11
    \$\begingroup\$ -ErrorAction Ignore would also ignore security errors. \$\endgroup\$ Commented Sep 7, 2018 at 19:55
8
\$\begingroup\$

It is better to use pipeline syntax. If there are no files, then nothing will happen:

Get-ChildItem $folder -Recurse | Remove-Item
Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
answered Jan 9, 2018 at 16:57
\$\endgroup\$
3
  • 1
    \$\begingroup\$ The -Recurse should be on Remove-Item if you want it to work correctly, otherwise the directories won't be deleted unless they have no files inside. Even that way though, this won't delete $folder, only its contents, so it's good but subtly different. \$\endgroup\$ Commented Feb 8, 2018 at 14:23
  • \$\begingroup\$ does not delete an empty $folder \$\endgroup\$ Commented Jun 15, 2018 at 12:39
  • 2
    \$\begingroup\$ Sorting the items by full path and then reversing the list would ensure that children get deleted before their parents, preventing nasty "Are you sure?" prompts. \$\endgroup\$ Commented Sep 7, 2018 at 19:57
4
\$\begingroup\$

This will delete an empty folder as well and still propagates errors if files cannot be deleted

Get-childItem .idea\caches -ErrorAction SilentlyContinue | Remove-Item -Recurse

The most correct option for exceptions would be the if (test-path $p) { rm $p } version

Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
answered Jun 15, 2018 at 12:41
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.