This Python script allows you to delete multiple repositories from your GitHub account based on a provided list of repositories to keep. Be cautious while using this script, as it will permanently delete repositories from your GitHub account.
For any questions or help, you can contact me on Telegram: t.me/mahmud_rafi
- Delete multiple GitHub repositories easily.
- Keep specific repositories safe from deletion.
- Simple and user-friendly command-line interface.
- Confirmation prompt before actual deletion to avoid accidents.
To use this script, you need to have the following installed:
- Python 3.x
requests
library
-
Create a GitHub Personal Access Token:
- Go to the GitHub settings page.
- Click on "Developer settings" in the left sidebar.
- From the developer settings page, click on "Personal access tokens."
- Click on "Generate new token."
- Enter your GitHub password if prompted.
- Give the token a meaningful name and select the appropriate scopes (permissions) based on what you need your script to do. For this script, you'll need at least "delete_repo" permission.
- Click on "Generate token."
- IMPORTANT: Copy the generated access token and save it in a safe place. Once you leave the page, you won't be able to see the token again.
-
Install PyGithub and requests:
- To interact with the GitHub API from Python, you'll need to install the PyGithub and requests library. You can install it using pip:
pip install PyGithub
pip install requests
- To interact with the GitHub API from Python, you'll need to install the PyGithub and requests library. You can install it using pip:
-
Create the Python Script:
- Open a text editor (e.g., Notepad, VSCode, Sublime Text) and create a new file.
- Copy and paste the following code into the file:
from github import Github
def delete_repositories(username, access_token, repos_to_keep):
# Create a GitHub instance using the access token
g = Github(access_token)
user = g.get_user(username)
# Get all repositories of the user
repos = user.get_repos()
# Loop through each repository and check if it should be deleted
for repo in repos:
if repo.name not in repos_to_keep:
# Delete the repository
repo.delete()
print(f"Deleted repository: {repo.name}")
if __name__ == "__main__":
# Replace "your_github_username" with your actual GitHub username
username = "your_github_username"
# Replace "YOUR_ACCESS_TOKEN" with the access token you generated
access_token = "YOUR_ACCESS_TOKEN"
# List the names of repositories you want to keep
repos_to_keep = ["Repo1", "Repo2", "Repo3"]
# Call the function to delete the unwanted repositories
delete_repositories(username, access_token, repos_to_keep)
-
Modify the Script:
- Replace
"your_github_username"
with your actual GitHub username. - Replace
"YOUR_ACCESS_TOKEN"
with the access token you generated in Step 1. - Populate the
repos_to_keep
list with the names of repositories you want to keep.
- Replace
-
Save the Script:
- Save the file with a meaningful name and the
.py
extension, e.g.,delete_repos.py
.
- Save the file with a meaningful name and the
-
Run the Python Script from Terminal or Command Prompt:
- Open Terminal or Command Prompt.
- Use the
cd
command to navigate to the directory where you saved the Python script. For example:
cd C:\Users\YourUsername\Scripts (Windows)
cd /Users/YourUsername/Scripts (macOS and Linux)
- Run the
Python
script using the python command followed by the script filename. For example:
python delete_repos.py
- The script will start executing, and if everything is set up correctly, it will delete the repositories as per the instructions provided in the script.
Remember to keep your access token secure and avoid sharing it with anyone or exposing it in public repositories. If you want to run this script periodically or on a server, consider using environment variables to store sensitive information securely.
For any questions or help, feel free to contact me on Telegram: t.me/mahmud_rafi