Anyone know a way to delete a workflow from GitHub Actions?

Solution 1:

As of July 7, 2020, you can now delete the results of individual workflow runs. To do this, navigate to your workflow, find the workflow run that you want to delete, and select the "..." menu. In this menu, select "Delete workflow run".

The workflow run and its logs will be removed.

Delete workflow run

Currently, you must do this for each workflow run individually.

edit: As of 2021 Feb it seems that after all workflow runs are deleted the workflow it self disappears. One comment below also seems to confirm this.

Solution 2:

It doesn't seem that there is currently a way to delete those workflows - this makes no sense - but it appears that once one makes the mistake of creating one they are stuck with it forever. The only solution so far I found is to disable these workflows.

So if I go to the Actions tab (edit the url to match your repo), I can then click on a workflow and disable it via [...] in the right top corner of that tab as in the snapshot below:

enter image description here

To delete all workflow results at once

To delete the records here is the solution I found here with slight modifications from the original:

user=GH_USERNAME repo=REPO_NAME; gh api repos/$user/$repo/actions/runs \
--paginate -q '.workflow_runs[] | select(.head_branch != "master") | "\(.id)"' | \
xargs -n1 -I % gh api repos/$user/$repo/actions/runs/% -X DELETE

Replace GH_USERNAME and REPO_NAME with the desired github username and repo name correspondingly.

This will delete all the old workflows that aren't on the master branch. You can further tweak this to do what you need.

Prerequisites:

  • You will find the latest gh version here.

Notes:

  • You may have to gh auth login if this is your first time using it
  • You may further change the command to gh api --silent if you prefer not to see the verbose output.
  • For the final xargs part of the command chain - the original used -J instead of -I, which is not supported by GNU xargs. -J results in a single command, and -I will execute the command for each records, so it's a bit slower.

Thank you to the OP on the community forum for sharing this in first place.

Solution 3:

Here's a few commands to quickly clean up your workflows.

You'll need the xargs, gh and jq CLI tools.

Depending on how many runs you have you'll have to execute the delete step multiple times because the GH API endpoints are paginated.

OWNER=<your user/org name>
REPO=<repo name>

# list workflows
gh api -X GET /repos/$OWNER/$REPO/actions/workflows | jq '.workflows[] | .name,.id'

# copy the ID of the workflow you want to clear and set it
WORKFLOW_ID=<workflow id>

# list runs
gh api -X GET /repos/$OWNER/$REPO/actions/workflows/$WORKFLOW_ID/runs | jq '.workflow_runs[] | .id'

# delete runs (you'll have to run this multiple times if there's many because of pagination)
gh api -X GET /repos/$OWNER/$REPO/actions/workflows/$WORKFLOW_ID/runs | jq '.workflow_runs[] | .id' | xargs -I{} gh api -X DELETE /repos/$OWNER/$REPO/actions/runs/{}

Solution 4:

Based on the @Giampaolo Rodolà answer (which worked for me), I created this simple shell script that does the job.

Disable the workflow you want to delete (via Github console) before executing this script.

org=<your org>
repo=<your repo>

# Get workflow IDs with status "disabled_manually"
workflow_ids=($(gh api repos/$org/$repo/actions/workflows | jq '.workflows[] | select(.["state"] | contains("disabled_manually")) | .id'))

for workflow_id in "${workflow_ids[@]}"
do
  echo "Listing runs for the workflow ID $workflow_id"
  run_ids=( $(gh api repos/$org/$repo/actions/workflows/$workflow_id/runs --paginate | jq '.workflow_runs[].id') )
  for run_id in "${run_ids[@]}"
  do
    echo "Deleting Run ID $run_id"
    gh api repos/$org/$repo/actions/runs/$run_id -X DELETE >/dev/null
  done
done

Outcome:

Listing runs for the workflow ID 5261185
Deleting Run ID 507553125
Deleting Run ID 507548002
Listing runs for the workflow ID 5261568
Deleting Run ID 525274011
Deleting Run ID 525264327
Deleting Run ID 525247443

Ensure to have Github client installed and required token permissions in Github.

Solution 5:

I managed to fix this (currently not possible via UI) by using "gh" CLI tool and reading REST API docs.

First, get all your workflows (these are the ones shown in the web UI -> Actions -> left column):

$ gh api repos/$YOUR_USER/$YOUR_REPO/actions/workflows
{
  "total_count": 2,
  "workflows": [
    {
      "id": 3611607,
      "name": "FreeBSD",
      ...
    },
    {
      "id": 4336318,
      "name": "MacOS",
      ...
    }
  ]
}

Use the ID of the workflow you want to delete (say 3611607) to get all of its individual runs:

$ gh api repos/$YOUR_USER/$YOUR_REPO/actions/workflows/3611607/runs
{
  "total_count": 17,
  "workflow_runs": [
    {
      "id": 363876785,
      "name": "FreeBSD",
      ...
    },
    {
      "id": 363876786,
      "name": "FreeBSD",
      ...
    },
    {
      "id": 363876787,
      "name": "FreeBSD",
      ...
    },
}

For each run id (let's say 363876785), delete it with:

$ gh api repos/$YOUR_USER/$YOUR_REPO/actions/runs/363876785 -X DELETE

After this, the undeletable Action in the left column of the web UI should disappear.