How to delete all file and folders with msbuild

The RemoveDir task removes the specified directories and all of its files and subdirectories. You don't have to remove the files and subdirectories first. Just pass the directory name to RemoveDir.

   <ItemGroup>
        <DirsToClean Include="work" />
    </ItemGroup>
    <Target Name="CleanWork">
        <RemoveDir Directories="@(DirsToClean)" />
    </Target>

While there are ways to construct this using just MSBuild, I'd highly recommend the MSBuild Extension pack.

http://msbuildextensionpack.codeplex.com/ [has been moved]
GitHub: MSBuildExtensionPack

Using the pack, you get a RemoveContent task that does exactly what you are needing. Once you install, you'd just do something like:

<MSBuild.ExtensionPack.FileSystem.Folder
   TaskAction="RemoveContent" Path="$(PathtoEmpty)"/>

Tags:

Msbuild