Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created December 19, 2023 07:25
Show Gist options
  • Save mahemoff/216518b8cf3bc28b08cfc2f6fb37c490 to your computer and use it in GitHub Desktop.
Save mahemoff/216518b8cf3bc28b08cfc2f6fb37c490 to your computer and use it in GitHub Desktop.
Clean up old codespaces

Deletes all but most recent Github codespaces, for a given repo.

Prerequisite: Install Github CLI, then auth as below:

gh auth refresh -h github.com -s codespace

#!/bin/bash
repo=$1
[[ $# -eq 0 ]] && echo "Error: Specify a repo (can be a filter pattern, not necessarily full 'user/repo')" >&2 && exit 1
old_codespaces=$(gh codespace list --json name,repository -q ".[] | select(.repository? | contains(\"$repo\")) | .name" | head -n -1)
for codespace in $(echo $old_codespaces); do
echo "Deleting $codespace"
gh codespace delete -c $codespace
done
echo DONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment