Removing Untracked Files Using git clean Command

Untracked files are the types of files that are present in the Git repository but have not been added yet to its tracking index. The untracked files are placed in the Git repository unstaging area and can be added to the tracking index through the “git add <filename>” command. However, an ignored untracked file comes under the category of a file that has not been recognized by Git and can be removed to free up space.

Git clean works fine with untracked files, but it has its limitations. Not all files can be removed using this command. For instance, you can’t remove recently created directories, files linked to an existing commit, historically tracked files, or files already added to the index. Besides, when using the git clean command, you must use the “-force” option. Otherwise, you will get an error message, and the command won’t work.

This tutorial will demonstrate:

Method 1: Removing Untracked Single File Using git clean Command

Untracked files are those files that have not been committed and added to the index of the Git repository. To delete these untracked files, you can utilize the “git clean -f” command.

Take a look at the provided procedure to practically implement the added scenario!

Step 1: Launch Git Terminal

Firstly, launch the Git Bash terminal through the Start menu:

Step 2: Open the Local Git Repository

Next, open the Git local directory using the “cd” command:

$ cd "C:\Git"

Step 3: Generate New File

Generate a new file through the “touch” command:

$ touch File1.txt

Step 4: Check Status

Verify the repository’s current status:

$ git status

The below output shows that a new file is successfully created and untracked:

Step 5: Remove Untracked File

Utilize the “clean” command along with the “-f” option to remove untracked files forcefully:

$ git clean -f

Step 6: Verify File Removal

Again, execute the “git status” command to check whether the untracked file is removed or not:

$ git status

From the below output, you can see that the untracked file “File1.txt” is nowhere:

Method 2: Removing Untracked Multiple Files Using git clean Command

To remove multiple untracked files, follow the below provided steps.

Step 1: View Repository Status

First, view the repository status to check whether any untracked files are present in the unstage area or not:

$ git status

It can be observed that two untracked files are displayed under the “Untracked files” category:

Step 2: Remove Multiple Untracked Files

To remove these untracked files, utilize the below provided Git command:

$ git clean -f

Again, checkout the Git repository status to verify if the untracked files are deleted or not:

$ git status

Here you go! All untracked files are deleted:

Method 3: Removing Untracked Directory Using git clean Command

The “git clean -f” command doesn’t delete untracked directories by default. However, adding the “-d” flag will do the magic. Furthermore, like in the case of files, first, you are required to see which untracked directories will get deleted. To do so, use the provided command:

$ git clean -dn

In our case, we see that we have one untracked directory:

Once you’ve confirmed so, proceed to delete the directory using the command:

$ git clean -df

Verify the deletion operation by running the “git status”:

$ git status

Here, you can see that there is no untracked file as well as directory:

Method 4: Removing Untracked Ignored Files Using git clean Command

The “-f” flag omits ignored files. Some .gitignore files are non-crucial files generated during compilation. If, after you’ve done a “dry-run”, you feel the need to delete them, add the “-x” flag when deleting folders or files, as it will help to remove untracked “.gitignore” files. For instance, to delete ignored files and untracked directories simultaneously, use the command:

$ git clean -dfx

Let’s create a “.gitignore” file in our workspace and use the “-f” to delete it. For this purpose, check out below given instructions.

Step 1: Create New File

To create the file, execute the following command in the Git Bash terminal:

$ touch ignorefile1.txt

Step 2: Ignore File

Next, to ignore the newly created file, add the file name into the “.gitignore” file using mentioned command:

$ echo ignorefile1.txt >> .gitignore

Run the “git status” to check if the untracked “.gitignore” file exists or not:

$ git status

Step 3: Remove Ignored Untracked File

Next, remove the ignored file as well as “.gitignore” using below command:

$ git clean -fx

Git Clean Interactive Mode

Using the “-i” flag activates the interactive mode, which is helpful to see what is happening before you delete the files. A “what now” prompt will display, requiring you to enter a command to proceed with any action. For instance, to see the untracked directories using the interactive mode, run the command:

$ git clean -dni

You can click any command to proceed, such as 5, then click the enter button to quit the prompt:

If you are unsure what each command in the prompt represents, choose option “6” to open the help page:

We have elaborated on the method for removing untracked files, ignore files, and directories using the git clean command.


To remove untracked files using the git clean command, first open the Git bash terminal. Then, move to the Git local repository. Then, create a new file that will remain untracked. After that, utilize the “$ git clean -f” command to delete a single untracked file or multiple files. Delete an untracked directory or folder through the “git clean” command along with the “-d” option. In this write-up, we have shown how to use the Git clean command to delete untracked files, directories, and the .gitignore file for deleting untracked ignored files.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.