Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2023-09-21T12:45:51.987Z
Answers: This does a clean of the workspace **before** your build runs, not after
Answers: Enrico
Answers: CC BY-SA 4.0
Answers: 2022-12-12T10:34:57.660Z
Answers: 2022-12-12T10:34:57.660Z
Answers: zerocukor287
Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2022-11-30T04:31:07.173Z
Answers: 2024-03-08T20:56:42.110Z
Answers: 2024-03-08T20:56:42.110Z
Answers: lioncub
Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2023-11-28T16:18:21.997Z
Answers: Would it not be quicker to skip the loop and just run `Remove-Item $(Build.SourcesDirectory) -Force -Recurse`
Answers: John
Answers: CC BY-SA 4.0
Answers: 2023-11-29T10:44:56.263Z
Answers: 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.
Answers: lg2de
Answers: CC BY-SA 4.0
Answers: 2023-09-18T13:24:26.047Z
Answers: 2023-11-29T10:42:38.333Z
Answers: 2023-11-29T10:42:38.333Z
Answers: lg2de
Body: <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>
  
Tags: <azure-pipelines>
Title: Azure DevOps Clean after pipeline run
UserDisplayName: zerocukor287
ThreadText: # 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](https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/jobs-job?view=azure-pipelines).
  
  > ```
  > 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
  
  ```yaml
  - 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:
  
  1. Using `failOnStderr` or not
  2. Using PowerShell or PowerShell Core (`pwsh: true`)
  3. Add separate tasks for `$(Build.ArtifactStagingDirectory)` and `$(Build.BinariesDirectory)`
  4. Clear complete working folder e.g. by using variable `$(Agent.BuildDirectory)`
  5. 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.  
  
  ---
  
  