116

Does anybody know a way to recursively remove all files in a working copy that are not under version control? (I need this to get more reliable results in my automatic build VMware.)

3
  • 7
    I'm an SVN user and have been comparing Git to SVN to see if I want to eventually want to make the switch. it looks like this may be another example where Git shines with its "git clean" command.
    – jpierson
    Sep 21, 2010 at 14:11
  • 3
    Or hg purge --all in Mercurial. Mar 6, 2012 at 17:58
  • Duplicate of stackoverflow.com/questions/2803823/… where there's a lot more useful activity. Sep 15, 2018 at 0:55

32 Answers 32

143

this works for me in bash:

 svn status | egrep '^\?' | cut -c8- | xargs rm

Seth Reno's is better:

svn status | grep ^\? | cut -c9- | xargs -d \\n rm -r 

It handles unversioned folders and spaces in filenames

As per comments below, this only works on files that subversion doesn't know about (status=?). Anything that subversion does know about (including Ignored files/folders) will not be deleted.

If you are using subversion 1.9 or greater you can simply use the svn cleanup command with --remove-unversioned and --remove-ignored options

21
  • 6
    Also usable in Windows in cygwin.
    – Honza
    May 4, 2009 at 16:56
  • 9
    You might consider adding the -d option to xargs for file names with spaces and the -r option to rm for any added directories: svn status | grep ^\? | cut -c9- | xargs -d \\n rm -r
    – Seth Reno
    Jan 26, 2011 at 19:34
  • 4
    I also had problems with the -d option running on OS X, my alternative is as follows, which translates the linebreaks into null chars and uses the -0 option on xargs to handle spaces in filenames: svn status | grep ^\? | cut -c9- | tr '\n' '\0' | xargs -0 rm Apr 15, 2011 at 22:19
  • 3
    If you frown upon commands that rely on the exact number of characters in the output of another command: svn status | grep "^?" | awk '{print $2}' | xargs -d \\n rm -r Apr 25, 2013 at 7:18
  • 3
    @Pavel Take a look at the xargs --no-run-if-empty option
    – Ken
    Sep 27, 2013 at 7:25
71

I ran across this page while looking to do the same thing, though not for an automated build.

After a bit more looking I discovered the 'Extended Context Menu' in TortoiseSVN. Hold down the shift key and right click on the working copy. There are now additional options under the TortoiseSVN menu including 'Delete unversioned items...'.

Though perhaps not applicable for this specific question (i.e. within the context of an automated build), I thought it might be helpful for others looking to do the same thing.

3
  • Great! On XP it only works in the list view (right side of explorer) not in the tree view (left side). Sep 29, 2010 at 15:06
  • Fantastic, only send it to recycle bin, would be nice to do a straight delete. Just what i needed. Nov 16, 2010 at 9:08
  • You can also automate this on the command line with TortoiseSVN's TortoiseProc.exe: details in my answer below.
    – stevek_mcc
    Sep 14, 2016 at 21:40
32

Edit:

Subversion 1.9.0 introduced an option to do this:

svn cleanup --remove-unversioned

Before that, I use this python script to do that:

import os
import re

def removeall(path):
    if not os.path.isdir(path):
        os.remove(path)
        return
    files=os.listdir(path)
    for x in files:
        fullpath=os.path.join(path, x)
        if os.path.isfile(fullpath):
            os.remove(fullpath)
        elif os.path.isdir(fullpath):
            removeall(fullpath)
    os.rmdir(path)

unversionedRex = re.compile('^ ?[\?ID] *[1-9 ]*[a-zA-Z]* +(.*)')
for l in  os.popen('svn status --no-ignore -v').readlines():
    match = unversionedRex.match(l)
    if match: removeall(match.group(1))

It seems to do the job pretty well.

3
  • 1
    Still works for me with Python 2.7.2. Warren P: Can you provider more details? Jun 23, 2014 at 10:42
  • I think it was just a problem with Python 2.6. Works again for me in 2.7.
    – Warren P
    Jun 23, 2014 at 14:12
  • 1
    Downvote: The other solution from bellow svn cleanup --remove-unversioned is better. And it is for Subversion 1.9.0 (this version is from 2015). It is stable and standar.
    – tres.14159
    Nov 21, 2018 at 8:19
19

See: svn-clean

3
  • Present in cygwin in the subversion-tools package.
    – bobbogo
    Mar 26, 2013 at 11:42
  • And in Ubuntu, in the same package: subversion-tools.
    – Tim
    Apr 25, 2013 at 18:32
  • This also removes the nested repositories. Sep 16, 2016 at 14:31
9

If you are on windows command line,

for /f "tokens=2*" %i in ('svn status ^| find "?"') do del %i

Improved version:

for /f "usebackq tokens=2*" %i in (`svn status ^| findstr /r "^\?"`) do svn delete --force "%i %j"

If you use this in a batch file you need to double the %:

for /f "usebackq tokens=2*" %%i in (`svn status ^| findstr /r "^\?"`) do svn delete --force "%%i %%j"
1
  • 1
    This kinda worked for me. Seemed to choke on some unversioned folders though.
    – jpierson
    Sep 21, 2010 at 14:06
7

I added this to my windows powershell profile

function svnclean {
    svn status | foreach { if($_.StartsWith("?")) { Remove-Item $_.substring(8) -Verbose } }
}
3
  • 2
    @FelipeAlvarez Yes. Yes, we do. It's not the greatest thing since sliced bread, but it beats batch. I'd say it's at least as useful as bash, probably a bit more so since you can pull in .NET assemblies.
    – jpmc26
    Mar 31, 2014 at 23:03
  • It suffers from microsoft's abominable tendency towards verbosity (not just in command name length, but in overall impossibility of getting anything done without copying giant snippets from the internet), but it's shockingly useful, and rather well thought-through.
    – Warren P
    Jun 23, 2014 at 14:13
  • 1
    You may want to add --no-ignore to svn status and -Recurse to Remove-Item Jan 7, 2015 at 15:03
5

Linux command line:

svn status --no-ignore | egrep '^[?I]' | cut -c9- | xargs -d \\n rm -r

Or, if some of your files are owned by root:

svn status --no-ignore | egrep '^[?I]' | cut -c9- | sudo xargs -d \\n rm -r

This is based on Ken's answer. (Ken's answer skips ignored files; my answer deletes them).

5

Just do it on unix-shell with:

rm -rf `svn st . | grep "^?" | cut -f2-9 -d' '`
1
  • This does not work if the number of to be deleted files exceeds the maximal number of command line arguments. See also the xargs based answers. Jul 22, 2014 at 20:30
5

Subversion 1.9.0 introduced option to remove unversioned items [1]

svn cleanup --remove-unversioned

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

4

Can you not just do an export to a new location and build from there?

2
  • 1
    For an automated build I would want a clean export.
    – g .
    Jun 30, 2009 at 14:52
  • 1
    Ideally, you would do this, but this is problematic if your checkout is very large. That is likely the reason the OP asked: to make the build shorter.
    – jpmc26
    Mar 31, 2014 at 23:06
4

If you have TortoiseSVN on your path and you are in the right directory:

TortoiseProc.exe /command:cleanup /path:"%CD%" /delunversioned /delignored /nodlg /noui

The options are described in the TortoiseSVN help for /command:cleanup:

Use /noui to prevent the result dialog from popping up either telling about the cleanup being finished or showing an error message). /noprogressui also disables the progress dialog. /nodlg disables showing the cleanup dialog where the user can choose what exactly should be done in the cleanup. The available actions can be specified with the options /cleanup for status cleanup, /revert, /delunversioned, /delignored, /refreshshell and /externals.

4

If you are using tortoise svn there is a hidden command to do this. Hold shift whilst right clicking on a folder to launch the context menu in windows explorer. You will get a "Delete Unversioned Items" command.

see the bottom of this page for details, or the screen shot below which highlights the extended features with the green stars, and the one of interest with the yellow rectangle...

SVN Extended context menu vs standard menu

3

My C# conversion of Thomas Watnedals Python script:

Console.WriteLine("SVN cleaning directory {0}", directory);

Directory.SetCurrentDirectory(directory);

var psi = new ProcessStartInfo("svn.exe", "status --non-interactive");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.WorkingDirectory = directory;

using (var process = Process.Start(psi))
{
    string line = process.StandardOutput.ReadLine();
    while (line != null)
    {
        if (line.Length > 7)
        {
            if (line[0] == '?')
            {
                string relativePath = line.Substring(7);
                Console.WriteLine(relativePath);

                string path = Path.Combine(directory, relativePath);
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                }
                else if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
        }
        line = process.StandardOutput.ReadLine();
    }
}
4
  • I would rather move the unversioned files, just in case you need them somewhere later.
    – leppie
    Oct 27, 2008 at 9:24
  • On a development machine, of course - but in the build VMware, that wouldn't make any sense cause nobody logs on to it and creates files. Oct 28, 2008 at 16:39
  • Thanks, I used this as part of my MSBuild script in cruisecontrol to clean up my source dir prior to builds
    – gregmac
    Nov 27, 2009 at 15:40
  • Started off based on your code and went a ways further: github.com/tgmayfield/svn-clean-sharp/downloads Oct 21, 2011 at 18:26
3
svn st --no-ignore  | grep '^[?I]' | sed 's/^[?I]  *//' | xargs -r -d '\n' rm -r

This is a unix shell command to delete all files not under subversion control.

Notes:

  • the st in svn st is an build-in alias for status, i.e. the command is equivalent to svn status
  • --no-ignore also includes non-repository files in the status output, otherwise ignores via mechanisms like .cvsignore etc. - since the goal is to have a clean starting point for builds this switch is a must
  • the grep filters the output such that only files unknown to subversion are left - the lines beginning with ? list files unknown to subversion that would be ignored without the --no-ignore option
  • the prefix up to the filename is remove via sed
  • the xargs command is instructed via -r to not execute rm, when the argument list would be empty
  • the -d '\n' option tells xargs to use a newline as delimiter such the command also works for filenames with spaces
  • rm -r is used in case complete directories (that are not part of the repository) need to be removed
2

I couldn't get any of the above to work without additional dependencies I didn't want to have to add to my automated build system on win32. So I put together the following Ant commands - note these require the Ant-contrib JAR to be installed in (I was using version 1.0b3, the latest, with Ant 1.7.0).

Note this deletes all unversioned files without warning.

  <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
  <taskdef name="for" classname="net.sf.antcontrib.logic.ForTask" />

  <macrodef name="svnExecToProperty">
    <attribute name="params" />
    <attribute name="outputProperty" />
    <sequential>
      <echo message="Executing Subversion command:" />
      <echo message="  svn @{params}" />
      <exec executable="cmd.exe" failonerror="true"
            outputproperty="@{outputProperty}">
        <arg line="/c svn @{params}" />
      </exec>
    </sequential>
  </macrodef>

  <!-- Deletes all unversioned files without warning from the 
       basedir and all subfolders -->
  <target name="!deleteAllUnversionedFiles">
    <svnExecToProperty params="status &quot;${basedir}&quot;" 
                       outputProperty="status" />
    <echo message="Deleting any unversioned files:" />
    <for list="${status}" param="p" delimiter="&#x0a;" trim="true">
      <sequential>
        <if>
          <matches pattern="\?\s+.*" string="@{p}" />
          <then>
            <propertyregex property="f" override="true" input="@{p}" 
                           regexp="\?\s+(.*)" select="\1" />
            <delete file="${f}" failonerror="true" />
          </then>
        </if>
      </sequential>
    </for>
    <echo message="Done." />
  </target>

For a different folder, change the ${basedir} reference.

1
  • 1
    Note: only deletes unversioned files; does not remove empty unversioned folders.
    – Steve
    Apr 23, 2009 at 12:45
2
svn status --no-ignore | awk '/^[I\?]/ {system("echo rm -r " $2)}'

remove the echo if that's sure what you want to do.

2
  • 1
    This is inferior to the xargs based answers because for n to be deleted files there are n /bin/sh and n rm processes forked. Jul 22, 2014 at 20:33
  • Agreed. Thanks for the xargs info.
    – Aria
    Jul 17, 2016 at 21:21
2

Since everyone else is doing it...

svn status | grep ^? | awk '{print $2}' | sed 's/^/.\//g' | xargs rm -R
1

Might as well contribute another option

svn status | awk '{if($2 !~ /(config|\.ini)/ && !system("test -e \"" $2 "\"")) {print $2; system("rm -Rf \"" $2 "\"");}}'

The /(config|.ini)/ is for my own purposes.

And might be a good idea to add --no-ignore to the svn command

1

I stumbled on svn-clean on my RH5 machine. Its located at /usr/bin/svn-clean

http://svn.apache.org/repos/asf/subversion/trunk/contrib/client-side/svn-clean

1

Pure windows cmd/bat solution:

@echo off

svn cleanup .
svn revert -R .
For /f "tokens=1,2" %%A in ('svn status --no-ignore') Do (
     If [%%A]==[?] ( Call :UniDelete %%B
     ) Else If [%%A]==[I] Call :UniDelete %%B
   )
svn update .
goto :eof

:UniDelete delete file/dir
if "%1"=="%~nx0" goto :eof
IF EXIST "%1\*" ( 
    RD /S /Q "%1"
) Else (
    If EXIST "%1" DEL /S /F /Q "%1"
)
goto :eof
1
  • Actually this script did not delete my files. Maybe due to spaces in it. The one-line answer by @SukeshNambiar did work. Sep 19, 2014 at 10:24
1

I've tried Seth Reno's version from this answer but it didn't worked for me. I've had 8 characters before filename, not 9 used in cut -c9-.

So this is my version with sed instead of cut:

svn status | grep ^\? | sed -e 's/\?\s*//g' | xargs -d \\n rm -r
1

If you're cool with powershell:

svn status --no-ignore | ?{$_.SubString(0,1).Equals("?")} | foreach { remove-item -Path (join-Path .\ $_.Replace("?","").Trim()) -WhatIf }

Take out the -WhatIf flag to make the command actually perform the deletes. Otherwise it will just output what it would do if run without the -WhatIf.

1

I would add this as a comment to Thomas Watnedal's answer , but can't yet.

A minor issue with it (which won't affect Windows) is that it only checks for files or directories. For Unix like systems where symbolic links may be present, it is necessary to change the line:

if os.path.isfile(fullpath):

to

if os.path.isfile(fullpath) or os.path.islink(fullpath):

to also remove links.

For me, changing the last line if match: removeall(match.group(1)) into

    if match:
        print "Removing " + match.group(1)
        removeall(match.group(1))

so that it displays what it is removing was useful too.

Depending on the use case, the ?[\?ID] part of the regular expression may be better as ?[\?I], as the D also removes deleted files, which were under version control. I want to use this to build in a clean, checked in folder, so there should be no files in a D state.

1

@zhoufei I tested your answer and here is updated version:

FOR /F "tokens=1* delims= " %%G IN ('svn st %~1 ^| findstr "^?"') DO del /s /f /q "%%H"
FOR /F "tokens=1* delims= " %%G IN ('svn st %~1 ^| findstr "^?"') DO rd /s /q "%%H"
  • You must use two % marks in front of G and H
  • Switch the order: first remove all files, then remove all directories
  • (optional:) In place of %~1 can be used any directory name, I used this as a function in a bat file, so %~1 is first input paramter
0

If you don't want to write any code, svn2.exe from svn2svn does this, also there's an article on how it's implemented. Deleted folders and files are put in the recycle bin.

Run "svn2.exe sync [path]".

0

For the people that like to do this with perl instead of python, Unix shell, java, etc. Hereby a small perl script that does the jib as well.

Note: This also removes all unversioned directories

#!perl

use strict;

sub main()

{

    my @unversioned_list = `svn status`;

    foreach my $line (@unversioned_list)

    {

        chomp($line);

        #print "STAT: $line\n";

        if ($line =~/^\?\s*(.*)$/)

        {

            #print "Must remove $1\n";

            unlink($1);

            rmdir($1);

        }

    }

}

main();
0

Using TortoiseSVN: * right-click on working copy folder, while holding the shift-key down * choose "delete unversioned items"

How can I delete all unversioned/ignored files/folders in my working copy?

0

A clean way to do this in PERL would be:

#!/usr/bin/perl
use IO::CaptureOutput 'capture_exec'

my $command = sprintf ("svn status --no-ignore | grep '^?' | sed -n 's/^\?//p'");

my ( $stdout, $stderr, $success, $exit_code ) = capture_exec ( $command );
my @listOfFiles = split ( ' ', $stdout );

foreach my $file ( @listOfFiles )
{ # foreach ()
    $command = sprintf ("rm -rf %s", $file);
    ( $stdout, $stderr, $success, $exit_code ) = capture_exec ( $command );
} # foreach ()
0

I used ~3 hours to generate this. It would take 5 mins to do it in Unix. The mains issue were: spaces in names for Win folders, impossibility to edit %%i and problem with defining vars in Win cmd loop.

setlocal enabledelayedexpansion

for /f "skip=1 tokens=2* delims==" %%i in ('svn status --no-ignore --xml ^| findstr /r "path"') do (
@set j=%%i
@rd /s /q !j:~0,-1!
)
0

C# code snipet above did not work for me - I have tortoise svn client, and lines are formatted slightly differently. Here is same code snipet as above, only rewritten to function and using regex.

        /// <summary>
    /// Cleans up svn folder by removing non committed files and folders.
    /// </summary>
    void CleanSvnFolder( string folder )
    {
        Directory.SetCurrentDirectory(folder);

        var psi = new ProcessStartInfo("svn.exe", "status --non-interactive");
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
        psi.WorkingDirectory = folder;
        psi.CreateNoWindow = true;

        using (var process = Process.Start(psi))
        {
            string line = process.StandardOutput.ReadLine();
            while (line != null)
            {
                var m = Regex.Match(line, "\\? +(.*)");

                if( m.Groups.Count >= 2 )
                {
                    string relativePath = m.Groups[1].ToString();

                    string path = Path.Combine(folder, relativePath);
                    if (Directory.Exists(path))
                    {
                        Directory.Delete(path, true);
                    }
                    else if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
                line = process.StandardOutput.ReadLine();
            }
        }
    } //CleanSvnFolder

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.