groovy script to delete artifacts on nexus 3 (not nexus 2)
Asked Answered
R

2

2

i have nexus 3 server that i save artifacts on it, and it has been filled to max. i wish to create a task to delete the old artifacts every day but always remain with at least 50 artifacts. the problem is that the default task that should do it, does't work. configuration part 1

configuration part 2

so i read that it can be done with a groovy script that i schedule to run inside tasks.

can anyone help me with it? i can't find anything useful on the internet.

Rifle answered 9/8, 2017 at 11:53 Comment(6)
Do you have 50 maven artifacts total or 50 different versions of the same artifact? It's not clear to me what you're trying to do nor why the task you showed doesn't work.Bubble
i have repository name production, and i want to save there only the last 50 artifact versions, so every day it'll remove the oldest artifact versions until there will be only 50 left. it's the same artifact, different builderRifle
How many snapshots do you output a day? More than 50?Bubble
too many.. but not as much as 50... but our developers have their days. the test repository has crazy uploading rateRifle
Okie. Can you describe why the task you listed above doesn't work? Or do you feel that's out of scope? I think it should work from what you've described of your scenario.Bubble
i really don't know why it won't work, i configured it, and for a week haven't check it. the i figure it didn't work at all.Rifle
A
9

based on @daniel-schröter answer you could add a Scheduled Task following this example:

Go to System -> Tasks and click Create Task. Create a script task:

enter image description here

Set the language to groovy and copy this script modified to fit to scheduled task (you should provide your own modifications to it, it's just an example):

import org.sonatype.nexus.repository.storage.Component
import org.sonatype.nexus.repository.storage.Query
import org.sonatype.nexus.repository.storage.StorageFacet

log.info("delete components for repository: my-repo")

def compInfo = { Component c -> "${c.group()}:${c.name()}:${c.version()}[${c.lastUpdated()}]}" }

def repo = repository.repositoryManager.get("my-repo")
StorageFacet storageFacet = repo.facet(StorageFacet)

def tx = storageFacet.txSupplier().get()
tx.begin()
Iterable<Component> components = tx.findComponents(Query.builder().where('last_updated < ').param('2190-01-01').build(), [repo])
tx.commit()
tx.close()

log.info("about to delete " + components.flatten(compInfo))
for(Component c : components) {
    log.info("deleting " + compInfo(c))
    tx2 = storageFacet.txSupplier().get()
    tx2.begin()
    tx2.deleteComponent(c)
    tx2.commit()
    tx2.close()
}

log.info("finished deleting " + components.flatten(compInfo))
Avignon answered 27/6, 2018 at 19:9 Comment(3)
I had problem executing the script with log.delete. I changed to log.info and it works. I use Nexus 3.12.1Rentfree
Yes i forgot to mention that, i've updated the answerAvignon
I used the script to delete a group. Then run the Tasks compacting blob storage followed by rebuilding metadata. Now I have no artifacts but many maven-metadata files. How I can delete these too?Cathexis
G
0

I stumbled upon the same problem. I really think that those features should be in nexus out of the box but the tasks for deleting old released artifacts etc. just wait for ages in nexus backlog. In the end I wrote some scripts to show how many artifacts are stored in which repo and how many per month etc. Then I wrote a script to delete old ones... You can probably use or extend this: https://github.com/danischroeter/nexus-repo-scripting

Gruver answered 5/10, 2017 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.