<p>There is a <code>clean</code> option within the job properties. See the <a href="https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/jobs-job?view=azure-pipelines" rel="nofollow noreferrer">job definition</a>.</p>
<blockquote>
<pre><code>jobs:
- job:
...
 workspace:  # Workspace options on the agent.
   clean: string # Which parts of the workspace should be scorched before fetching.  (outputs, resources, all)
</code></pre>
</blockquote>
<p>As it would be called before fetching, there will be always space for the new build.</p>

CC BY-SA 4.0
2023-09-21T12:45:51.987Z
This does a clean of the workspace **before** your build runs, not after
Enrico
CC BY-SA 4.0
2022-12-12T10:34:57.660Z
2022-12-12T10:34:57.660Z
zerocukor287
<pre class="lang-yaml prettyprint-override"><code>- task: DeleteFiles@1
  displayName: 'clean *'
  inputs:
    SourceFolder: '$(Pipeline.Workspace)'
    Contents: '**/*'
    RemoveDotFiles: true
  condition: always()
</code></pre>
<p>will clean everything in '$(Pipeline.Workspace)/notClean/<strong>allClean</strong>'.</p>

CC BY-SA 4.0
2022-11-30T04:31:07.173Z
2024-03-08T20:56:42.110Z
2024-03-08T20:56:42.110Z
lioncub
<p>I tried to use <code>DeleteFiles@1</code>, like @lionclub. But the implementation is very slow for large repositories.</p>
<p>So, I decided to use PowerShell:</p>
<pre><code>- task: PowerShell@2
  displayName: 'Cleanup source folder'
  inputs:
    targetType: 'inline'
    script: 'Get-ChildItem -Recurse | foreach { Write-Host $_.FullName ; Remove-Item $_.FullName -Force -Recurse }'
    failOnStderr: true
    pwsh: true
    workingDirectory: '$(Build.SourcesDirectory)'
</code></pre>
<p>There are variations:</p>
<ol>
<li>Using <code>failOnStderr</code> or not</li>
<li>Using PowerShell or PowerShell Core (<code>pwsh: true</code>)</li>
<li>Add separate tasks for <code>$(Build.ArtifactStagingDirectory)</code> and <code>$(Build.BinariesDirectory)</code></li>
<li>Clear complete working folder e.g. by using variable <code>$(Agent.BuildDirectory)</code></li>
<li>Adding <code>condition: always()</code> to always clear folder, incl. failed runs</li>
</ol>

CC BY-SA 4.0
2023-11-28T16:18:21.997Z
Would it not be quicker to skip the loop and just run `Remove-Item $(Build.SourcesDirectory) -Force -Recurse`
John
CC BY-SA 4.0
2023-11-29T10:44:56.263Z
This is maybe another variation. My version reports progress.
Please note that I reverted your edit and converted it into variation because I think that failed runs should not be cleared by default.
lg2de
CC BY-SA 4.0
2023-09-18T13:24:26.047Z
2023-11-29T10:42:38.333Z
2023-11-29T10:42:38.333Z
lg2de
<p>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.</p>
<p><strong>What is the preferred way of cleaning the agents' work directories?</strong></p>
<p>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.</p>
<p>We tried to schedule a deletion task every day, but it doesn't solve our problems.</p>
<p>We tried to add a script at the end of our <code>yaml</code> file that cleans the working dir. Is it recommended?</p>
<pre><code>    - powershell: Remove-Item $(Pipeline.Workspace)/** -Force
      displayName: Clean after publish
</code></pre>
<p>Also it would be great if we could clean up even if the build fails, or someone cancels it midway.</p>

<azure-pipelines>
Azure DevOps Clean after pipeline run
zerocukor287
# Azure DevOps Clean after pipeline run

(Asked by: username_0 on 2021-11-25)

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.

**Tags:** azure-pipelines


---

## 3 answers

### Answer by username_0 on 2022-12-12. Score: 1 | Accepted answer

There is a clean option within the job properties. See the job definition.

jobs:
- job:
...
 workspace:  # Workspace options on the agent.
   clean: string # Which parts of the workspace should be scorched before fetching.  (outputs, resources, all)


As it would be called before fetching, there will be always space for the new build.

> **Comments:**
> **username_1**: This does a clean of the workspace **before** your build runs, not after  

---

### Answer by username_2 on 2022-11-30. Score: 2

- task: DeleteFiles@1
  displayName: 'clean *'
  inputs:
    SourceFolder: '$(Pipeline.Workspace)'
    Contents: '**/*'
    RemoveDotFiles: true
  condition: always()

will clean everything in '$(Pipeline.Workspace)/notClean/allClean'.


---

### Answer by username_3 on 2023-09-18. Score: 2

I tried to use DeleteFiles@1, like @lionclub. But the implementation is very slow for large repositories.
So, I decided to use PowerShell:
- task: PowerShell@2
  displayName: 'Cleanup source folder'
  inputs:
    targetType: 'inline'
    script: 'Get-ChildItem -Recurse | foreach { Write-Host $_.FullName ; Remove-Item $_.FullName -Force -Recurse }'
    failOnStderr: true
    pwsh: true
    workingDirectory: '$(Build.SourcesDirectory)'

There are variations:

Using failOnStderr or not
Using PowerShell or PowerShell Core (pwsh: true)
Add separate tasks for $(Build.ArtifactStagingDirectory) and $(Build.BinariesDirectory)
Clear complete working folder e.g. by using variable $(Agent.BuildDirectory)
Adding condition: always() to always clear folder, incl. failed runs

> **Comments:**
> **username_4**: Would it not be quicker to skip the loop and just run `Remove-Item $(Build.SourcesDirectory) -Force -Recurse`  
> **username_3**: This is maybe another variation. My version reports progress. Please note that I reverted your edit and converted it into variation because I think that failed runs should not be cleared by default.  

---

