DEV Community

Cover image for Automatically delete old GitHub Actions runs
Pooyan Razian
Pooyan Razian

Posted on • Originally published at pooyan.info

Automatically delete old GitHub Actions runs

Originally written at pooyan.info

Who is the author? Check out my profile on LinkedIn.

In this article we will learn an efficient way to delete old GitHub Actions runs.

GitHub Actions is a great tool for automating your workflow. However, if you have not set a retention policy for your runs, you may end up with a lot of old runs that you do not need anymore. In this article, I will show you how to delete old GitHub Action runs.

GitHub Actions runs are the executions of your workflows. They can be triggered by pushing a commit or creating a pull request, or on a schedule, etc. based on how you have defined it in your workflow config files. Those config files act as a recipe or a blueprint and each run is an instance of that recipe. (similar to class vs object if you are a programmer) Each time a workflow is triggered, a new run is created, and you can see the list of runs in the Actions tab of your repository.

How to delete old GitHub Actions runs

One way to delete old logs and artifacts is to globally set a retention policy for all repositories in your organization. This can be done by going to the organization settings and set this: Set retention policy for all repositories in an organization The problem with this approach is that you cannot granularly set the retention policy for each repository. Also, it needs to be done by an org-level admin. So, if you do not have the access to set this policy for your org, or you want to set a different retention you should either manually do it, by opening the Actions tab of your repository and deleting them all manually one by one, which can be time-consuming, or you should build your own solution to call the GitHub API to list and delete them automatically. This is not a one-time job, so you end up having to do this again and again.

To save you time, I have created a public Action, available on the GitHub Marketplace that you can use for deleting old Actions runs. It can be found: here.

Right now, in this initial version, it accepts the following inputs:

  • token: The GitHub token to use for authentication. (required)
  • days-ago: The number of days ago to delete the runs. (required)
  • dry-run: If set to true, the Action will only list the runs to be deleted without actually deleting them. (optional, default: false)
  • keep-latest: The number of latest runs to keep. (optional, default: 0)

For the token, you can just use the {{ secrets.GITHUB_TOKEN }} which is a default secret that GitHub provides to each run. You can read more about it here.

You can use the dry-run option to test the accuracy of the Action before trusting it to delete your runs. And you can use the keep-latest option to keep the latest runs, even if they are older than the days-ago value. This can be useful if you want to keep the latest runs for a specific reason.

Here is an example workflow that uses the Action to delete runs older than 30 days:

on:
  schedule:
    - cron: '0 0 * * *'  # Specify your own schedule

jobs:
  delete-old-actions:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4 
      - uses: yanovation/delete-old-actions@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          days-ago: 30
Enter fullscreen mode Exit fullscreen mode

And when it runs, you will see the list of deleted runs in the job log:

An example run

The only cons of this approach that I can imagine is that it consumes additional minutes from your limits, but that is not a big deal in almost all cases. To minimize this, you can even schedule this workflow to run once a month or so, but bear in mind that this will make the deletion dates less accurate.

Do you already use this Action?

Do you know any other solutions?

Do you have any suggestions?

Let me know.


If you liked the article and want to keep me motivated to provide more content, you can share this article with your friends and colleagues and follow me here on Medium or LinkedIn.

Copyright & Disclaimer

  • All content provided on this article is for informational and educational purposes only. The author makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site.
  • All the content is copyrighted, except the assets and content I have referenced to other people's work, and may not be reproduced on other websites, blogs, or social media. You are not allowed to reproduce, summarize to create derivative work, or use any content from this website under your name. This includes creating a similar article or summary based on AI/GenAI. For educational purposes, you may refer to parts of the content, and only refer, but you must provide a link back to the original article on this website. This is allowed only if your content is less than 10% similar to the original article.
  • While every care has been taken to ensure the accuracy of the content of this website, I make no representation as to the accuracy, correctness, or fitness for any purpose of the site content, nor do I accept any liability for loss or damage (including consequential loss or damage), however, caused, which may be incurred by any person or organization from reliance on or use of information on this site.
  • The contents of this article should not be construed as legal advice.
  • Opinions are my own and not the views of my employer.
  • English is not my mother-tongue language, so even though I try my best to express myself correctly, there might be a chance of miscommunication.
  • Links or references to other websites, including the use of information from 3rd-parties, are provided for the benefit of people who use this website. I am not responsible for the accuracy of the content on the websites that I have put a link to and I do not endorse any of those organizations or their contents.
  • If you have any queries or if you believe any information on this article is inaccurate, or if you think any of the assets used in this article are in violation of copyright, please contact me and let me know.

Top comments (0)