Tag Archives: Visual Studio

How to Automate Cleaning Up a .NET Solution

Motivation:

You have a .NET solution with many projects.
You need to deliver the solution to a client very frequently.
You need to clean up all intermediate folders and files to reduce the package size and make the result look tidy.
You want to automate this process to reduce cleaning up time.

Solution:

  1. In Visual Studio, review Project Build Order… and a last project in the list.
  2. Right click the project and select Properties.
  3. Click on Build Events.
  4. Enter the following commands to the Post-build event command line text box:
    rd /s /q $(ProjectDir)obj
    cd $(TargetDir)
    del *.config
    del *.pdb
    del *.xml

    rd /s /q $(ProjectDir)obj: Remove the obj folder in the project directory.
    cd $(TargetDir): Move to the output directory.
    del *.config: Delete all the config files in the output directory.
    del *.pdb: Delete all the pdb files in the output directory.
    del *.xml: Delete all the xml files in the output directory.

    You can modify these commands or add new commands for your specific purpose.

  5. Save the project.
  6. Repeat the process for some other projects in the solution if necessary. Typically you only need to clean up intermediate folders and files for the some projects in the bottom of the build order list. You may use the Dependencies tab to change the build order for some projects.

Cleaning a folder manually:

In order to delete all the obj folders recursively you can

    1. Create DeleteObjFolders.bat file inside the same folder that contains your solution file (i.e. *.sln)
    2. Paste content below to the DeleteObjFolders.bat file:
    @echo off
    @echo Deleting all OBJ folders...
    for /d /r . %%d in (obj) do @if exist "%%d" rd /s/q "%%d"
    @echo OBJ folders successfully deleted :) Press any key to close the window.
    pause > nul

    3. Execute the DeleteObjFolders.bat file.