This Python script automates the process of deleting unused GitHub Codespaces, helping to free up resources and manage your development environment efficiently.
Requirements • Usage • Script • Disclaimer • Diagrams
To run this script, you need:
- Python 3.x
- GitHub Personal Access Token with appropriate permissions
- The following Python packages (install using
pip install -r requirements.txt
):- requests
- python-dotenv
- Go to GitHub Settings > Developer Settings > Personal access tokens
- Click on "Generate new token" (classic)
- Give your token a descriptive name
- Set the expiration as needed
- Select the following scopes:
codespace
: Full control of codespacesrepo
: Full control of private repositories (needed to check branch status)
- Click "Generate token"
- Copy the generated token (you won't be able to see it again)
Make sure to keep your token secure and never share it publicly.
- Clone the repository:
git clone https://github.com/ronknight/delete-unused-codespaces.git
cd delete-unused-codespaces
- Install the required dependencies:
pip install -r requirements.txt
- Create a
.env
file in the project root and add your GitHub credentials:
GITHUB_ACCESS_TOKEN=your_access_token_here
GITHUB_USERNAME=your_github_username
- Run the script:
python delete-unused-codespaces.py
The main script delete-unused-codespaces.py
performs the following actions:
- Fetches all codespaces for the authenticated user
- Checks each codespace for pending changes
- Deletes codespaces without pending changes
- Saves deletion status and codespace information to JSON files
Key functions:
get_codespaces()
: Fetches all codespacesget_branch_status()
: Checks for pending changes in a repositorydelete_codespace()
: Deletes a specific codespacesave_deletion_status_to_json()
: Saves deletion status to a JSON file
IMPORTANT: This script will delete all unused codespaces without prompting for confirmation.
- Use this script with extreme caution.
- Ensure you have backups of any important work stored in your codespaces.
- The script considers a codespace "unused" if it doesn't have pending changes. This may not always align with your definition of "unused".
- Once a codespace is deleted, its contents cannot be recovered.
- The author is not responsible for any data loss, unintended deletions, or any other negative consequences resulting from the use of this script.
- By using this script, you acknowledge that you understand the risks and accept full responsibility for any outcomes.
It is strongly recommended to review the list of codespaces manually before running this script, especially if you're using it for the first time.
USE AT YOUR OWN RISK.
Made with ❤️ by Ronknight