Skip to content

Instantly share code, notes, and snippets.

@salehi
Created August 10, 2020 13:39
Show Gist options
  • Save salehi/32e8514f45c69f6393696bbdd2876f95 to your computer and use it in GitHub Desktop.
Save salehi/32e8514f45c69f6393696bbdd2876f95 to your computer and use it in GitHub Desktop.
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
@salehi
Copy link
Author

salehi commented Aug 10, 2020

after running this script, you should delete unreferenced registry layers by running following cmd:

gitlab-ctl registry-garbage-collect -m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment