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.

29 thoughts on “Delete Unversioned Files Under SVN”

  1. You can do a similar thing on Windows using PowerShell:

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

  2. 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. 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. Don’t forget to escape those spaces!

    svn status --no-ignore | grep '^\?' | sed 's/^\?      //' | sed 's/ /\\ /g' | xargs rm -rf
  5. 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. 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. How about just cutting the 8 first characters?

    svn stat --no-ignore | grep ^\? | cut -b 8- | xargs rm
    
    
    				
  8. @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. why not just using awk?

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

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

  11. 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. 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.

  13. 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

  14. 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.

Leave a Reply

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