226

Is it possible to delete all untracked files from my working directory? Let's say I added a bunch of files to my working directory, didn't add them via 'hg add' and now want to get rid of those new files entirely?

I'm on windows, although I'm using PowerShell, so a combined solution is also possible here.

tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
Valentin V
  • 24,971
  • 33
  • 103
  • 152

11 Answers11

308

Add the Mercurial Extension called purge. It is distributed by Mercurial.

This extension adds a “purge” command to “hg” that removes files not known to Mercurial. i.e. untracked Files. So your command would be,

hg purge

It is not enabled by default, maybe to avoid accidentally removing files that you forgot to add.

To install this extension, add this to your mercurial settings file (.hgrc on Unix, Mercurial.ini on Windows)

[extensions]
purge = 

To enable this extension temporarily you can use

hg purge --config extensions.purge= 
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
simplyharsh
  • 35,488
  • 12
  • 65
  • 73
  • 27
    `hg purge --all` will delete all un-tracked and ignored files. This is useful for cleaning up in-source builds – tacaswell Oct 27 '12 at 02:48
  • 23
    to enable the ext temporarily you can use ```hg purge --config extensions.purge=``` – Pykler Dec 30 '13 at 01:32
128

The proper way without purge is:

hg st -un0 | xargs -0 rm
tonfa
  • 24,151
  • 2
  • 35
  • 41
  • 1
    @simplyharsh is this way better or did you just find it less work than adding the extension? – Yaakov Kuperman Apr 16 '12 at 17:14
  • 3
    @YaakovKuperman I guess adding extension is lesser work than this command (one time enabling). I do not enable purge extension because I need it less than once in a fortnight. Also call me silly but, typing this command gives me enough time to think again before doing something as desctructive as 'purge'. – simplyharsh Apr 17 '12 at 12:08
  • 2
    @simplyharsh , that makes sense, but there's two reasons I'd go for purge all the same. First, you can do hg purge --print and see a list of what its going to get rid of before you do the purge. Second, if you do it this way you need to be in the root of the repo for it to work. – Yaakov Kuperman Apr 17 '12 at 13:43
  • If you don't have any uncommitted modifications that you want to keep, you can also do `hg up 0; rm -r *; hg up tip` or whatever revision you're currently on. – javawizard Mar 01 '13 at 23:50
  • 1
    @javawizard: This is a nice one that made me rethink how hg works. However, it only works because `rm -rf *` skips dot files and dot directories, including `.hg/`. By the same token, any other dotfile (say .evil-settings) will also survive and purge is better. – dirkjot Jan 28 '14 at 07:55
  • 4
    Be aware that there is a difference in behavior between what 'hg st -u' and 'hg purge' finds. 'hg st -u' WILL NOT find untracked empty directories, while 'hg purge' WILL find (and remove) them. That may or may not be important to you. – Michael Kent Mar 12 '15 at 18:45
20

Thanks! This worked for me also in Powershell:

hg st -un | rm
Nikolaos Georgiou
  • 2,792
  • 1
  • 26
  • 32
4

You can use

hg purge --all

to remove all the ignored and untracked files

(first you need to install the purge extension as explained in some answers)

Alex L
  • 1,069
  • 2
  • 18
  • 33
3
rm $(hg st -u)

...where -u stands for "untracked" you can also pick another state.

Alex
  • 8,245
  • 8
  • 46
  • 55
3

Try following:

hg st -un | xargs rm
Alexei Tenitski
  • 9,030
  • 6
  • 41
  • 50
2

This should do the trick:

hg status | grep '^\?' | sed 's/^\? //' | xargs rm -rf
Kristian
  • 6,357
  • 4
  • 36
  • 37
2

if you don't want to use purge:

rm $(hg st | grep ^? | awk '{print $2}')
Lev
  • 921
  • 7
  • 15
0

Assuming that you are using a *nix system you could run something like this:

rm `hg st | awk '/\?/ {print $2}'`

from the root of the mercurial repository.

I don't know of a standard mercurial command to achieve the same but I believe there are many more command-line options to do this. I'm sure there are "better" solutions and would be interested to hear any other suggestions.

Please use this command with caution as it was not thoroughly tested.

Heinrich Filter
  • 5,760
  • 1
  • 33
  • 34
0

This works from Windows 10 command line (used cautiously of course):

for /f %g in ('hg status -un') do @echo %g & @del %g
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
0

A quick/hacky way, if you do not have local changes, is to delete the folders you want from the file manager (Windows explorer for example) and then use "hg revert" which restores only the tracked files.

pthomaid
  • 146
  • 2
  • 7