Skip to content

Instantly share code, notes, and snippets.

@absk1317
Created September 16, 2019 09:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save absk1317/39bd317f320aa9117415626786b8e264 to your computer and use it in GitHub Desktop.
Save absk1317/39bd317f320aa9117415626786b8e264 to your computer and use it in GitHub Desktop.
delete old gitlab artifacts
require 'httparty'
token = 'YOUR_TOKEN'
project_id = 'PROJECT_ID'
query = { per_page: 100 }
headers = { "PRIVATE-TOKEN" => token }
server = "https://gitlab.com/api/v4/projects/#{project_id}/jobs"
response = HTTParty.get(server, query: query, headers: headers)
total_pages = response.to_hash['x-total-pages'].first.to_i
puts "total pages: #{total_pages}"
job_ids = []
# skipping page 1
(2..total_pages).each do |i|
puts "Processing Page: #{i}/#{total_pages}"
jobs = HTTParty.get(server, query: query.merge(page: i), headers: headers)
jobs.each do |job|
if job['artifacts_file'] && job['artifacts_file']['filename']
puts "artifact found for job #{job['id']}"
job_ids << job['id']
end
end
end
puts "#{job_ids.count} Jobs found. Commencing removal of Artifacts.."
# Loop through each Job erasing the Artifact(s)
job_ids.each do |job_id|
response = HTTParty.post("#{server}/#{job_id}/erase", query: {}, headers: headers)
puts "Processing Job ID: #{job_id} - Status: #{response['status']}"
end
puts "Finished!"
@adriensck
Copy link

Thanks for this script!

If you only need to delete artifacts and not the entire job data, you can change line 32 request by this one:

response = HTTParty.delete("#{server}/#{job_id}/artifacts", query: {}, headers: headers)

I just changed POST action by a DELETE one and /erase by /artifacts, according to GitLab API Documentation.

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