Delete Unversioned Files Under SVN

Sometimes svn switch fails because unversion files exist in the working copy, and the need to erase them comes up. This is also true when updating to an old revision (usually one that misses directories that exist in the head revision), doing some work (like compiling), and then trying to update back to the head revision. Subversion (SVN) will fail complaining that directories/files already exist.

The cause of the problem is that the working-copy isn’t clean, it has some (or a lot) of unversioned files. The best solution will be to delete all of them. While this can be a tedious task when done manually for each file, it can be achieved with single shell command.

svn status --no-ignore | grep '^\?' | sed 's/^\?      //'
svn status --no-ignore | grep '^\?' | sed 's/^\?      //'  | xargs -Ixx rm -rf xx

The first command will list all unversioned files and directories. Use this to review the files to be deleted. After you review the files, use the second command to delete them easily.

UPDATE 2010-12-29: Fixed the command, so it will handle spaces in filenames correctly.

28 thoughts on “Delete Unversioned Files Under SVN

  1. Damian Powell

    You can do a similar thing on Windows using PowerShell:

    (svn stat “–no-ignore”) -match ‘^[I?]‘ -replace ‘^.\s+’,” | rm

  2. Yumi Nanako

    RE: Damian Powell

    Tried (svn stat “–no-ignore”) -match ‘^[I?]‘ -replace ‘^.\s+’,” | rm

    but it didn’t quite work for me

    (svn status –no-ignore) -match ‘^[?]‘ -replace ‘^.\s+’ | rm

    works fine for me though.

  3. Guy Post author

    Yes it does, and unfortunately I still don’t know of a way around it in the comments that works perfectly. However in most occasions putting the code inside pre tags (and setting lang attribute to whatever you want) gives you preformatted syntax highlighted code. For example:

    (svn status --no-ignore) -match '[?]' -replace '^.\s+' | rm
  4. Ivan

    Don’t forget to escape those spaces!

    svn status --no-ignore | grep '^\?' | sed 's/^\?      //' | sed 's/ /\\ /g' | xargs rm -rf
  5. joao

    To properly deal with spaces one has to do:

    svn status --no-ignore | grep '^\?' | sed 's/^\? //' | sed 's/\([^^]\) /\1\\ /g' | xargs rm

    in mac osx at least…

  6. joao

    scratch my last comment, it’s Ivan’s way and should read


    svn status --no-ignore | grep '^\?' | sed 's/^\? //' | sed 's/ /\\ /g' | xargs rm

    or something like that… Jeez this isn’t easy…

  7. Lionel

    How about just cutting the 8 first characters?

    svn stat --no-ignore | grep ^\? | cut -b 8- | xargs rm
    
    
    							
  8. Guy Post author

    @Lionel, that indeed seems to be a very nice solution. But I think it should be like this

    svn stat --no-ignore | grep ^\? | cut -b 9- | xargs rm

    I’ve replaced the 8 with a 9 as you want to start printing from the 9th byte to the eol.

  9. tobi

    why not just using awk?

    svn status –no-ignore|awk ‘/^\?/ {print $2}’|xargs rm

  10. Guy Post author

    awk is a great tool, but I lack comprehensive knowledge of it, thus I usually prefer other tools.

  11. Andy

    Piping lines into xargs rm here *will* give you grief if you have spaces in your file names!

    This is safer:
    (svn status | grep ‘^[\?~]‘| cut -c 9- )| while read VICTIM; do
    rm -fr “$VICTIM”
    echo “Deleted $VICTIM”
    done

  12. Pingback: Optimus Pete - svn: delete unversioned files

  13. Baz

    Late to the party but… Andy, you’re right that spaces in filenames will cause problems if you pass them to xargs, but they’re not the only problematic character. The usual way to fix this is to use tr and the -0 flag on xargs, eg:

    ls|tr ‘\n’ ”|xargs -0 rm

    It’s what -0 is there for.

  14. Baz

    Ugh, the blog mangled my command. The tr command was

    tr space single-quote backslash n single-quote space single-quote backslash zero single-quote

  15. Pingback: Delete Unversioned Files Under SVN by Guy Rutenberg – Code Cafe

  16. Pingback: Fabio Brandão – Blog » Blog Archive » Excluindo arquivos não versionados no SVN

  17. Josh Greco

    Thank you very much for this. I’ve been doing it manually, one file at a time for a very long time now and it only just occurred to me to search for an automated solution.

    Seriously, you have saved me so much time. Thank you.

  18. Pingback: Andreas' Blog » Blog Archive » Subversion

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>