53

How can I remove all untracked files from an SVN checkout with the svn command line tool? Git and Hg offer clean and purge commands for this purpose, but I can't find the corresponding command in SVN.

I don't care if I have to revert all my local modifications in the process (in which case I'd essentially want to restore my checkout to a pristine state) or whether local modifications to tracked files can remain intact; either situation is acceptable.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Possible duplicate of [How can I delete all unversioned/ignored files/folders in my working copy?](https://stackoverflow.com/questions/2803823/how-can-i-delete-all-unversioned-ignored-files-folders-in-my-working-copy) – Ruslan Sep 04 '17 at 18:55
  • @Ruslan: Even though my question isn't about TortoiseSVN, yes, that's essentially the same point. – Kerrek SB Sep 04 '17 at 19:10

4 Answers4

64

There may be a built-in way, but if so, I don't know of it. However, something like the following should do the job:

svn st | grep '^?' | awk '{print $2}' | xargs rm -rf

(All unversioned files should appear in a line beginning with ? in an svn st.)

EDIT (@GearoidMurphy) It's challenging to get this snippet to work on a cygwin environment, as xargs treats the windows path slashes as escape sequences and has trouble parsing the '\r\n' line endings, below is my adapted solution of this perfectly valid answer:

svn st | grep '^?' | gawk '{printf(\"%s|\", $2)}' | xargs -d "|" -n1 C:\cygwin\bin\rm -r
Community
  • 1
  • 1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Yeah, I suppose that may have to do, though it's formidably ugly, and it requires several extra non-SVN tools. – Kerrek SB May 02 '12 at 13:21
  • 2
    This doesn't take account of ignored files, so you may also need the `--no-ignore` flag to `svn st` – the_mandrill May 02 '12 at 15:03
  • 4
    Also I think this may perhaps fail for filenames containing spaces – the_mandrill May 02 '12 at 15:22
  • 7
    This will work for filenames with spaces: `svn st | grep '^?' | awk '{print $2}' | xargs -I{} rm -rf '{}'` – Nick Garvey Oct 30 '12 at 17:17
  • 1
    To remove the ignored files, you'd need to use `svn st --no-ignore | grep '^I' | awk '{print $2}' | xargs rm -rf`. `svn st --no-ignore` starts the line with an `I` for ignored files. – Kat Sep 17 '14 at 05:28
  • I was googling on how to remove the untracked files with `svn` keeping in mind `awk` and was hoping to find a better solution. Didn't work out apparently. – iurii Jan 07 '15 at 14:42
  • To properly handle filenames with space, I had to make one more change to @NickGarvey's snippet: `svn st | grep '^?' | awk '{$1=""; print $0}' | xargs -I{} rm -rf '{}'`. Note changed `awk` command based on http://stackoverflow.com/questions/2961635/using-awk-to-print-all-columns-from-the-nth-to-the-last. – Ilya Kurnosov Aug 25 '15 at 13:48
  • If no `-v` is given, there are exactly 9 columns before the path. So an alternative is `svn st | grep '^?' | cut -c9- | while read fname; do rm -rf "$fname"; done` (using while allows for multiple commands, even conditionals for each file) – Narcolessico Jun 04 '19 at 07:44
50

Since version 1.9, you can use the following:

svn cleanup . --remove-unversioned

See https://subversion.apache.org/docs/release-notes/1.9.html#svn-cleanup-options

Julio Batista Silva
  • 1,861
  • 19
  • 19
20

The svn-clean script does precisely this, though it does rely on having perl installed. It will leave local modifications intact but will remove ignored files. Run with '-p' to preview the files that will be removed.

the_mandrill
  • 29,792
  • 6
  • 64
  • 93
4

This command should do the job, spaces and all (tried on OSX only):

svn status | grep '^\?' | sed 's/? *//' | xargs -I%  rm -fr %
Rodrigo Lopez
  • 871
  • 10
  • 16