Deleting Multiple GitHub Repositories Using a Python Script

Introduction

GitHub is a widely used platform for version control and collaborative development, allowing developers to host, manage, and share their code repositories. While it’s common to create new repositories, there are times when you may need to delete multiple repositories, whether to clean up your account, remove outdated projects, or for any other reason. Manually deleting each repository can be time-consuming and tedious, especially if you have many to remove. Automating this task with a Python script can save time and effort. This article will guide you through the process of deleting multiple GitHub repositories using a Python script, leveraging GitHub’s REST API.

Prerequisites

Before diving into the script, ensure you have the following:

  • GitHub Personal Access Token (PAT): GitHub uses PATs to authenticate API requests. You can generate a PAT from your GitHub account settings, ensuring it has the necessary scopes (at least repo and delete repo).
  • Python Installed: Make sure Python is installed on your machine, along with the request’s library, which will be used for making HTTP requests.

Python Script to Delete Multiple Repositories

The following script demonstrates how to delete multiple GitHub repositories using a Python script. This example assumes you have a list of repository names that you want to delete.

import os
import requests
import sys

organization = sys.argv[1] # or user
token = sys.argv[2] # token needs delete_repo permission

url = 'https://api.github.com/repos/{}'.format(organization)

headers = {'Accept': 'application/vnd.github.v3+json',
           'Authorization': 'token {}'.format(token)}


lines = [line.strip() for line in open('todelete.txt')] # todelete.txt is a txt with repository names. One per line.

for repo in lines:
    print(os.path.join(url, repo))
    myresponse = requests.delete(os.path.join(url, repo), headers=headers)
    if myresponse.status_code == 204:
        print(f"Successfully deleted {repo}.")
    elif myresponse.status_code == 404:
        print(f"Repository {repo} not found.")
    else:
        print(f"Failed to delete {repo}: {myresponse.status_code} {myresponse.text}")

GitHub Action Pipeline

I’ve created GitHub Action pipeline as well, which will automatically trigger once you check-in anything in the main branch (Mostly by updating todelete.txt file).  You need to update list of GitHub repositories you need to delete in todelete.txt file and it will trigger the GitHub action pipeline and delete those repositories.

Source Code

I’d published the source code and GitHub action pipeline in this public repository. You can fork it and use it for your own purpose.

How the Script Works

  • Authentication: The script uses Basic Authentication, combining your GitHub username and PAT. This is necessary to authorize the deletion of repositories.
  • API Endpoint: The GitHub API endpoint for deleting a repository is DELETE /repos/:owner/:repo. The script constructs this URL dynamically for each repository in the list.
  • HTTP DELETE Request: For each repository, the script sends a DELETE request to the corresponding API endpoint. It then checks the response status:
    • 204 No Content: Indicates successful deletion.
    • 404 Not Found: Indicates the repository was not found, perhaps due to a typo or incorrect repository name.
    • Other status codes may indicate different issues, and the script outputs the status and error message for further investigation.

Conclusion

Automating the deletion of multiple GitHub repositories can be a significant time-saver, especially for developers managing numerous projects. This Python script provides a straightforward way to handle bulk deletions by leveraging the GitHub API. However, exercise caution when deleting repositories, as this action is irreversible. Always double-check the list of repositories to avoid accidentally removing important projects.

This script can also be extended or modified to include additional functionality, such as deleting repositories based on specific criteria (e.g., age, size, or activity level). With GitHub’s comprehensive API and Python’s versatility, automating tasks like these can greatly enhance your productivity and streamline your development workflow.



Leave a Reply

Your email address will not be published. Required fields are marked *

*