47

I can delete files with specific extensions in multiple folders with this:

Get-childitem * -include *.scc -recurse | remove-item

But I also need to delete folders with a specific name - in particular those that subversion creates (".svn" or "_svn") when you pull down files from a subversion repo.

1
  • 1
    For those who want to remove all .svn folders: perhaps svn export is of use to you. It will do a checkout but without adding the .svn folders. Commented Oct 29, 2019 at 13:25

3 Answers 3

83

This one should do it:

get-childitem -Include .svn -Recurse -force | Remove-Item -Force -Recurse

Other version:

$fso = New-Object -com "Scripting.FileSystemObject"
$folder = $fso.GetFolder("C:\Test\")

foreach ($subfolder in $folder.SubFolders)
{
    If ($subfolder.Name -like "*.svn")
    {
        remove-item $subfolder.Path -Verbose
    }       
}
Sign up to request clarification or add additional context in comments.

1 Comment

You might get "Cannot remove item directory not empty" kind of errors on the first few times. But when you keep trying the above command, it works.
16

I tend to avoid the -Include parameter on Get-ChildItem as it is slower than -Filter. However in this instance (since it can't be expressed as a -Filter), this is what I would use:

Get-ChildItem . -Include .svn,_svn -Recurse -Force | Remove-Item -Recurse -Force

or if typing this at the prompt:

ls . -inc .svn,_svn -r -fo | ri -r -fo

6 Comments

Not a typo but the wildcard character ? wasn't working as I expected. It should have matched both _ and . but it wasn't matching the .. Go figure.
I'm confused @Keith. You said you avoid -include, but that's what you used.
I said I "tend" to avoid -include for the reason stated above. However, if you need to filter based on several different wildcard specs, -filter won't handle that. So using -include should be no slower than piping through Where-Object to filter on the filename.
@MikeShepard (and Keith) I took the liberty of editing in some of Keith's comment into the body of the answer as it had me puzzling too! I guess best to leave both comments (and this) for future generations...
I got errors using this, presumably because it tries to call Get-ChildItem on folders that are already deleted, so I added -ErrorAction SilentlyContinue to the ri or Remove-Item command to mute it.
|
15

For fast deletion, use -Filter

To recursively delete a folder with a specific name -Filter is significantly faster than -Include.

So, for visibility, here is the faster version of @Keith's answer:

Fully typed-out:

Get-ChildItem -Filter .svn -Recurse -Force | Remove-Item -Recurse -Force

Short version:

ls . -filter .svn -r -fo | ri -r -fo

2 Comments

hm - is there also a version which outputs the paths deleted? just so i know it works and all.
Just used it for deleting git folders in an archive. worked perfectly. thanks. So to remove .git folders recursively run ls . -filter .git -r -fo | ri -r -fo

Your Answer

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.