Skip to content

Instantly share code, notes, and snippets.

@ZeroDeth
Forked from epelc/cleanup-images.md
Created July 23, 2018 14:49
Show Gist options
  • Save ZeroDeth/c8736a376d1c4289f1cb4963a94ff1d4 to your computer and use it in GitHub Desktop.
Save ZeroDeth/c8736a376d1c4289f1cb4963a94ff1d4 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.

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