How to properly clean up self-hosted runners?

Hi,
Is there any way to properly clean-up self-hosted runners on each job execution?
I can see files are present from the previous run in _work// folder which causing inconsistency.
I am looking for a way to clean up everything when job execution finishes.

Thanks.

@martyrs,

Currently, GitHub does not provide any build-in methods to clean everything in the _work directory after the job completed.
If you want to clean the _work directory, you can try to setup a step at the end of the job to use the remove command, such as rm (bash) or Remove-Item (PowerShell), to remove files in the _work directory.

- name: Clean working directory
  shell: bash
  run: |
    cd $RUNNER_WORKSPACE
    cd ..
    rm -r *

However, use the commands can’t completely remove the GITHUB_WORKSPACE (_work/repos/repos) in the _work directory. The files and subfolders in GITHUB_WORKSPACE can be removed, but the folder “_work/repos/repos” will be retained and you will get the error like as “cannot remove ‘repos/repos’: Device or resource busy”.

1 Like

As a quick fix, just add this job before any jobs

  cleaner:
    runs-on: self-hosted
    steps:
      - name: Runner workspace path
        run: |
          echo "Cleaning up previous run"
          rm -rf "${{ github.workspace }}"