Skip to content

Instantly share code, notes, and snippets.

@epelc
Last active October 17, 2019 15:14
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save epelc/42ed31a43d57a7fb3bd6c75379d0e1aa to your computer and use it in GitHub Desktop.
Save epelc/42ed31a43d57a7fb3bd6c75379d0e1aa to your computer and use it in GitHub Desktop.
Delete Gitlab Registry Images

Soft-delete the images

Generate a script to delete all images using the following. Copy paste into chrome console on the registry page of a project.

NOTE you must update the textToSave template string before running.

// Options(fill these out with your info)

// GITLAB_INSTANCE is the url to your custom instance or `gitlab.com`
const GITLAB_INSTANCE = 'gitlab.yourwebsite.com'
const GROUP = 'yourGroup'
const PROJECT = 'yourProjectSlug'

let images = []

$('.content-list tbody tr').each(function(index){
  let image = $(this).find('td')[0].innerText
  // Remove the space on the end.
  image = image.slice(0,-1)
  if (image.includes('-') && !image.includes('latest')) {
    images.push(image)
  }
})

console.log(`Found ${images.length} images`)

// Open chrome dev tools and go delete an image. Right click the request under the network tab and click "Copy as curl" then paste here.
// Replace the branch name in the url with `${branch}`
let textToSave = images.map(branch=> `curl 'https://${GITLAB_INSTANCE}.com/${GROUP}/${PROJECT}/container_registry/${branch}' //lots of headers`).join('\n')

let hiddenElement = document.createElement('a');

hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave);
hiddenElement.target = '_blank';
hiddenElement.download = 'remove-all-images.sh';
hiddenElement.click();

Run the script

cd ~/Downloads
chmod +x ./remove-all-images.sh
./remove-all-images.sh

Delete the images from the disk

Run garbage collection on registry then restart the registry.
docker exec gitlab gitlab-ctl registry-garbage-collect && docker restart gitlab

NOTE you have to restart the registry because the gc only removes the files, it doesn't notify the running registry to remove items from it's in memory cache.

@tomfun
Copy link

tomfun commented Apr 12, 2019

AAaaa !
It is blowminding.
Thanks.
Are there any other "normal" solution for this problem?

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