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.

Ori Wiesel
  • 488
  • 2
  • 8
  • 26
  • 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. – joedragons Aug 15 '17 at 19:18
  • 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 builder – Ori Wiesel Aug 23 '17 at 08:55
  • How many snapshots do you output a day? More than 50? – joedragons Aug 23 '17 at 14:50
  • too many.. but not as much as 50... but our developers have their days. the test repository has crazy uploading rate – Ori Wiesel Aug 29 '17 at 11:20
  • 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. – joedragons Aug 29 '17 at 15:28
  • 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. – Ori Wiesel Aug 31 '17 at 05:51

2 Answers2

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))
Sebastian
  • 4,770
  • 4
  • 42
  • 43
  • 1
    I had problem executing the script with `log.delete`. I changed to `log.info` and it works. I use Nexus 3.12.1 – Nicolas Pepinster Jul 03 '18 at 11:42
  • Yes i forgot to mention that, i've updated the answer – Sebastian Jul 03 '18 at 11:46
  • 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? – Marcel D-B Aug 29 '18 at 14:05
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