Last active
          August 14, 2022 16:44 
        
      - 
      
 - 
        
Save kedwards/78ba56b8b964941eb7ca3fe2b059b1e7 to your computer and use it in GitHub Desktop.  
    Delete GitHub workflows
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/env bash | |
| set -e | |
| opts=':hor:' | |
| script_name=${0##*/} | |
| script_dir="$( cd "$( dirname "$0" )" && pwd )" | |
| script_path=$script_dir/$script_name | |
| script_date=$(date +%Y%m%d) | |
| org=withreach | |
| usage() { | |
| echo "This script will remove all workflow runs" | |
| echo "for a repository for all disabled workflows" | |
| echo "$1" | |
| echo "Usage: $script_name <options>" | |
| echo "options:" | |
| echo " -o | gitub organization name" | |
| echo " -r | gitub repository" | |
| echo " -h | print this help menu" | |
| exit 1 | |
| } | |
| while getopts $opts opt | |
| do | |
| case "$opt" in | |
| n) org=$OPTARG | |
| ;; | |
| n) repo=$OPTARG | |
| ;; | |
| h) usage | |
| ;; | |
| \?) | |
| usage "Error: invalid parameter $OPTARG" | |
| ;; | |
| esac | |
| done | |
| shift $(($OPTIND - 1)) | |
| if [ -z "$repo" ]; then | |
| usage "Error: You must specify a repository name" | |
| fi | |
| echo Date: $script_date | |
| echo Script Directory: $script_dir | |
| echo Script Path: $script_path | |
| echo Repository: $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 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment