So, I ran into the problem of running out of disk space, as our releases / assemblies were stacked, so I worked a bit on creating a script to remove old collections and got the following:
import org.sonatype.nexus.repository.storage.StorageFacet; import org.sonatype.nexus.repository.storage.Query; def repositoryName = 'Integration'; def maxArtifactCount = 20; // Get a repository def repo = repository.repositoryManager.get(repositoryName); // Get a database transaction def tx = repo.facet(StorageFacet).txSupplier().get(); try { // Begin the transaction tx.begin(); def previousComponent = null; def uniqueComponents = []; tx.findComponents(Query.builder().suffix(' ORDER BY group, name').build(), [repo]).each{component -> if (previousComponent == null || (!component.group().equals(previousComponent.group()) || !component.name().equals(previousComponent.name()))) { uniqueComponents.add(component); } previousComponent = component; } uniqueComponents.each {uniqueComponent -> def componentVersions = tx.findComponents(Query.builder().where('group = ').param(uniqueComponent.group()).and('name = ').param(uniqueComponent.name()).suffix(' ORDER BY last_updated DESC').build(), [repo]); log.info(uniqueComponent.group() + ", " + uniqueComponent.name() + " size " + componentVersions.size()); if (componentVersions.size() > maxArtifactCount) { componentVersions.eachWithIndex { component, index -> if (index > maxArtifactCount) { log.info("Deleting Component ${component.group()} ${component.name()} ${component.version()}") tx.deleteComponent(component); } } } } } finally { // End the transaction tx.commit(); }
This works through the repository, looking for all the components. Then it works through all versions (sorted by the last updated - I could not determine the order by the version number, but I think it should be good), and then deletes any of them by the maxArtifactCount maximum number.
I hope this comes in handy - and if you see any questions, let me know.
source share