How to Automate Cleaning Up a .NET Solution


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 or files to reduce the package size and make the result look tidy.
You want to automate this process to reduce cleaning up time.

  1. In Visual Studio, right click a project and select Properties.
  2. Click on Build Events.
  3. 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.

  4. Save the project.
  5. Repeat the process for all the projects in the solution.

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.