I have self-hosted build agents using Azure DevOps. We have the clean flag in the checkout step, however we run out of space sometimes.

**What is the preferred way of cleaning the agents' work directories?**

The problem is, that the clean process is in the beginning of a pipeline, and leaves everything behind. This is even worse if we have two parallel builds. (First clean then clones the repo, then the second cleans and clones the repo, then first build, then there is no space for the second build). Unless the first build is scheduled again, the build outputs stays on the agent.

We tried to schedule a deletion task every day, but it doesn't solve our problems.

We tried to add a script at the end of our `yaml` file that cleans the working dir. Is it recommended?

```
    - powershell: Remove-Item $(Pipeline.Workspace)/** -Force
      displayName: Clean after publish

```

Also it would be great if we could clean up even if the build fails, or someone cancels it midway.