forked from microsoft/vcpkg
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from daschuer/delete_all_packages
Add delete_all_packages.py to delete all packages from a repository
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# This script allows you to mass delete all packages from a 'user' repository. | ||
# This only deletes 30 packages at a time. Repeat it until everything is deleted. | ||
|
||
import requests | ||
|
||
import netrc | ||
|
||
# Configure your GitHub user name and Personal Access Token (PAT) to the `.netrc` file. | ||
user, _, your_token = netrc.netrc().authenticators("api.github.com") | ||
|
||
def delete_packages(pat, user): | ||
headers = { | ||
'Authorization': f'Bearer {pat}', | ||
'X-GitHub-Api-Version': '2022-11-28', | ||
'Accept': 'application/vnd.github+json' # Required header for package deletion API | ||
} | ||
|
||
# Fetching package versions | ||
response = requests.get(f'https://api.github.com/users/{user}/packages?package_type=nuget', headers=headers) | ||
response.raise_for_status() | ||
|
||
print(response.json()) | ||
|
||
packages = response.json() | ||
|
||
# Deleting each package | ||
for package in packages: | ||
package_name = package.get('name') | ||
delete_url = f'https://api.github.com/users/{user}/packages/nuget/{package_name}' | ||
delete_response = requests.delete(delete_url, headers=headers) | ||
if delete_response.status_code == 204: | ||
print(f"Package {package_name} deleted successfully.") | ||
else: | ||
print(f"Failed to delete package {package_name}: {delete_response.status_code}") | ||
|
||
if __name__ == "__main__": | ||
delete_packages(your_token, user) | ||
|