Created
August 10, 2020 13:39
-
-
Save salehi/32e8514f45c69f6393696bbdd2876f95 to your computer and use it in GitHub Desktop.
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
from typing import List | |
import os | |
# pip install python-gitlab | |
from gitlab import Gitlab | |
from gitlab.v4.objects import Project, ProjectRegistryRepository, GitlabDeleteError | |
if __name__ == '__main__': | |
gl = Gitlab(os.environ['GITLAB_URL'], private_token=os.environ['TOKEN']) | |
projects: List[Project] = gl.projects.list(all=True) | |
for project in projects: | |
repositories: List[ProjectRegistryRepository] = project.repositories.list() | |
for repository in repositories: | |
print(f'Cleaning {repository.location}') | |
try: | |
repository.tags.delete_in_bulk(name_regex=".*", keep_n=5, older_than="1w") | |
except GitlabDeleteError as e: | |
if 'once an hour' not in str(e): | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
after running this script, you should delete unreferenced registry layers by running following cmd:
gitlab-ctl registry-garbage-collect -m