Skip to content

Instantly share code, notes, and snippets.

@kongzii
Created February 14, 2025 20:02
Show Gist options
  • Save kongzii/ba04aa747b8d68184e518878f51de6a0 to your computer and use it in GitHub Desktop.
Save kongzii/ba04aa747b8d68184e518878f51de6a0 to your computer and use it in GitHub Desktop.
Script to remove all docker images from github except one
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