How would one go about deleting all Subversion files from a directory using PowerShell?
-
7perhaps this isn't applicable, but it is possible to create a copy of your code from your repository without the subversion artifacts present. e.g. using TortoiseSVN, you can use 'SVN export' – Adam Ralph Feb 05 '10 at 20:33
-
maybe this helps https://devblogs.microsoft.com/scripting/how-can-i-use-windows-powershell-to-delete-all-the-tmp-files-on-a-drive/ – Ferroao Dec 02 '21 at 17:51
4 Answers
If you really do want to just delete the .svn directories, this could help:
gci c:\yourdirectory -include .svn -Recurse -Force |
Remove-Item -Recurse -Force
Edit:
Added -Force
param to gci
to list hidden directories and shortened the code.
Keith is right that it you need to avoid deleting files with .svn extension, you should filter the items using ?
.

- 28,745
- 11
- 71
- 104
-
is there a reason why we cannot recursively exclude the svn files while copying? I tried Copy-Item $source -Destination $dest -Recurse -Exclude '.svn'. But that didnt work. So I copied it entirely and then removed the .svn files recursively using this command. – gprasant Nov 23 '11 at 05:58
-
It doesn't work. I guess it is the same case as if you use `Get-ChildItem dir -recurse -exclude ..`. `-exclude` (and as I remember correctly also `-include`) doesn't play well with `-recurse`. – stej Nov 23 '11 at 14:43
-
just in case someone finds this and has a similar issue. this answer helped me delete a ton of thumbs.db files and clean up my diff view. I like the alias better though "dir * -recurse -include thumbs.db | del -recurse -force" – Brian Boatright Feb 05 '12 at 06:41
-
When I tried it the answer gci did not like the -force option - but still seems to have worked without it (Powershell 4.0) – Nigel Ellis Mar 29 '14 at 16:11
Assuming you don't want to delete any files that might also have .svn extension:
Get-ChildItem $path -r -include .svn -Force | Where {$_.PSIsContainer} |
Remove-Item -r -force
Microsoft has responded to the suggestion in the comments below that Keith opened on MS Connect! As of PowerShell V3 you can do away with the extra (very slow) pipe to Where {$_.PSIsContainer}
and use -directory
instead:
gci $path -r -include .svn -force -directory | Remove-Item -r -force
PowerShell v3 can be downloaded for Windows 7 at Windows Management Framework 3.0.

- 30,738
- 21
- 105
- 131

- 194,368
- 42
- 353
- 369
-
Yes, good catch :) However, I don't assume there is some file with this extension. – stej Feb 05 '10 at 22:46
-
I've never used Subversion (just ClearCase & TFS) so I wasn't sure. – Keith Hill Feb 05 '10 at 23:09
-
2BTW I look forward to the day when you don't need `Where {$_.PSIsContainer}` and all you have to do is `gci . -r -containerOnly`. I have my Get-ChildItem proxied to work this way but I can't count on other folks having it. :-( – Keith Hill Feb 05 '10 at 23:13
-
Have you added a suggestion about `-containerOnly` to connect? I would vote for sure. – stej Feb 05 '10 at 23:17
-
I'd lose the wildcard on the pattern and just do ".svn" - otherwise you'll get folders that end with .svn – Duncan Smart Feb 05 '10 at 23:20
-
2Stej, yes vote on it here: https://connect.microsoft.com/PowerShell/feedback/details/308796/add-enumeration-parameter-to-get-childitem-cmdlet-to-specify-container-non-container-both – Keith Hill Feb 05 '10 at 23:35
-
Ah, so the directories aren't named like foo.svn, they are literally called ".svn"? How about files? Are there files called just ".svn" or do they have names like "foo.svn"? – Keith Hill Feb 05 '10 at 23:49
-
Keith, both you and stej included the -recurse argument in the remove-item cmdlet. Is that really necessary? I tried it without and it worked which seems logical since remove-item is going to operate on what it is passed through the pipeline? But the fact that both of you included it in your example gives me pause. Am I missing something or am I right that the second recurse is not needed? Seth – Seth Spearman Oct 24 '11 at 14:44
-
I don't think it is needed. I probably added it just out of habit. :-) – Keith Hill Oct 24 '11 at 23:07
-
@SethSpearman You can leave the -r out of the remove-item pipe, but doing so will result in an "are you sure you want to recursively remove items? [y] [n]" warning EACH time it recurses...so you are better off explicitly adding the -r. – Bradley Mountford Jan 10 '13 at 14:03
How about using SVN Export to get a clean checkout without .svn directories?
Edit
You might want to look at the answer here:
Command line to delete matching files and directories recursively

- 1
- 1

- 761
- 4
- 6
The answer by stej is correct unless you want to delete all file with a specific string in their name. In that case, you should use:
gci "your_path" -include \*"specific_string"\* -Recurse -Force | Remove-Item -Recurse -Force
Notes:
your_path
only requires quotes if it contains spaces or special characters.
specific_string
requires quotes to make sure both wildcards are recognized, especially if the string includes characters such as ()
.

- 2,351
- 10
- 23
- 28

- 11
- 2