Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script that can be used to purge nexus v3 releases
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
def fmt = DateTimeFormat.forPattern('yyyy-MM-dd HH:mm:ss');
].each { reponame ->
// Get a repository
def repo = repository.repositoryManager.get(reponame);
// Get a database transaction
def tx = repo.facet(StorageFacet).txSupplier().get();
// Search assets that haven't been downloaded for more than three months
try {
// Begin the transaction
.where('last_downloaded <')
.build(), [repo]).each { asset ->
if (asset.componentId() != null) {
def component = tx.findComponent(asset.componentId());
if (component != null) {
def count = tx.countComponents(Query.builder().where('name').eq('version >').param(component.version()).build(), [repo]);
// Check if there is newer components of the same name
if (count > 0) {"Delete asset ${} as it has not been downloaded since 3 months and has a newer version")
} else {"Asset ${} wasn't downloaded for more than 3 months, I hope we dont' need it anymore even if this is the only version.")
// End the transaction
} catch (all) {"Exception: ${all}")
} finally {
Copy link

Thanks! Just what I was looking for. Adjusted it a bit with more logging and to my use case:

Copy link

Good evening,

I am not able to pass the repository to perform the cleaning, could you help me?

How does this script clean these components?

Thank you

Copy link

Very useful thanx.

Copy link

Thank you for this script !

Just curious, is there any way these components can be archived/copied to some other location before deleting , is it doable ?

Copy link

tgagor commented Feb 27, 2022

Hi @gopal9590

This script use internals of Nexus which were barely documented. I have no idea if it will be possible to extend it as you want.

Other approach might be to write for ex. Python script that will do something similar via REST API, but if I remember some date (like time creation or usage) were only accessible via internal scripts.

I can only wish you good luck ;)

Copy link

Appreciate your quick response , Thanks Tom !

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