How to remove untracked files from a Perforce working tree?

Viewed 24760

What's the equivalent of "git clean" with Perforce?

git-clean - Remove untracked files from the working tree

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

-x Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.

-X Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

11 Answers

In Windows:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
IF "%1"=="" ( 
    SET proceed=N
    ECHO Are you sure you wish to nuke all files under: 
    ECHO %CD%
    SET /P proceed="Y/N: "
    ECHO !proceed!
    IF /I "!proceed!"=="Y" (
        ECHO Nuking %cd%
        FOR /F "tokens=1 delims=-#" %%G IN ('p4 reconcile -nlad -f ...') do (
            DEL /F "%%G"
        )
    )
) ELSE IF /I "%1"=="-n" (
    ECHO Files to be deleted 1>&2
        FOR /F "tokens=1 delims=-#" %%G IN ('p4 reconcile -nlad -f ...') do (
            ECHO "%%G"
        )
) ELSE GOTO :USAGE

GOTO :STOP

:USAGE
ECHO Usage: nuke [-n]
ECHO     -n will only write the files to be deleted but will not actually delete them

:STOP

This one works much better:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
IF "%1"=="" ( 
    SET proceed=N
    ECHO Are you sure you wish to nuke all files under: 
    ECHO %CD%
    SET /P proceed="Y/N: "
    ECHO Looking for files to be deleted.  This may take some time depending on the depth of your tree. 1>&2
    IF /I "!proceed!"=="Y" (
        ECHO Nuking %cd%
        FOR /F "tokens=1 delims=#" %%G IN ('p4 reconcile -nla -f ...') DO (
            DEL /F %%G
        )
    )
) ELSE IF /I "%1"=="-n" (
    ECHO Looking for files to be deleted.  This may take some time depending on the depth of your tree. 1>&2
    ECHO Files to be deleted 1>&2
    FOR /F "tokens=1 delims=#" %%G IN ('p4 reconcile -nla -f ...') DO (
        ECHO %%G
    )
) ELSE GOTO :USAGE

GOTO :STOP

:USAGE
ECHO Usage: nuke [-n]
ECHO     -n will only write the files to be deleted but will not actually delete them

:STOP
Related