Created
February 14, 2025 20:02
-
-
Save kongzii/ba04aa747b8d68184e518878f51de6a0 to your computer and use it in GitHub Desktop.
Script to remove all docker images from github except one
This file contains hidden or 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
import subprocess | |
import json | |
def delete_versions() -> None: | |
# Fetch all version IDs | |
result = subprocess.run( | |
[ | |
"gh", | |
"api", | |
"--paginate", | |
f"/orgs/{ORG_NAME}/packages/container/{REPOSITORY}/versions", | |
], | |
capture_output=True, | |
text=True, | |
) | |
# Parse the JSON output to extract version IDs and tags | |
versions = json.loads(result.stdout) | |
version_ids = [ | |
version["id"] | |
for version in versions | |
if "main" | |
not in version.get("metadata", {}).get("container", {}).get("tags", []) | |
] | |
# Delete each version except those tagged as 'main' | |
for version_id in version_ids: | |
print(f"Deleting version ID: {version_id}") | |
subprocess.run( | |
[ | |
"gh", | |
"api", | |
"--silent", | |
"--method", | |
"DELETE", | |
f"/orgs/{ORG_NAME}/packages/container/{REPOSITORY}/versions/{version_id}", | |
] | |
) | |
ORG_NAME = "orgname" | |
REPOSITORY = "repo" | |
delete_versions() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment