artifactoryjfrog-cli

Jfrog artifactory delete folder of containing artifacts after remove them


I´m using this spec file to delete old artifacts of more than 3 months.

{
    "files": [{
        "aql": {
            "items.find": {
                "$or": [{
                    "$and": [{
                        "repo": "repo1",
                        "created": {
                            "$before": "3mo"
                        }
                    }],
                    "$and": [{
                        "repo": "repo2",
                        "created": {
                            "$before": "3mo"
                        }
                    }],
                    "$and": [{
                        "repo": "repo3",
                        "created": {
                            "$before": "3mo"
                        }
                    }]
                }]
            }
        }
    }]
}

but I want to remove the hole folders that begins with: "2019*" like is showed in the picture which contains the artifacts, and no just the artifacts in the folder.

enter image description here

I tried with following but I didn´t work:

 "items.find": {
          "repo": "repo1",
          "path": "com/domain/name",
          "name": {"$match":"20*"},
          "type": "folder",

Says no artifact founded.

Also is there a way with Jfrog to delete all artifacts except last 2? Just want to keep last 2 instead of having all and have to delete them with this script every certain time

Thanks!!

FOR more than one repo?

{
    "files": [{
        "aql": {
            "items.find": {
                "$or": [{
                    "$and": [{
                        "repo": "repo1",
                        "path": "com/foo/bar",
                        "created": {
                            "$before": "3mo"
                        }
                        "type": "folder",
                        "name": {"$match":"20*"}
                    }],
                    "$and": [{
                        "repo": "repo2",
                        "path": "com/foo/bart",
                        "created": {
                            "$before": "3mo"
                        }
                        "type": "folder",
                        "name": {"$match":"20*"}
                    }],
                    "$and": [{
                        "repo": "repo3",
                        "path": "com/foo/bar",
                        "created": {
                            "$before": "3mo"
                        }
                        "type": "folder",
                        "name": {"$match":"20*"}                    
                    }]
                }]
            }
        }
    }]
}

Solution

  • One of the most recommended tools for performing such operations in Artifactory is JFrog CLI. In order to delete the files and folders you want, you can use the CLI delete command. Together with the FileSpec you wrote, your command should be like:

    jfrog rt del --spec <PATH-TO-SPEC-FILE>
    

    To verify the files to delete, you can either run the search command prior to deleting:

    jfrog rt s --spec <PATH-TO-SPEC-FILE>
    

    or let the delete command output the list of files it finds.

    For deleting the folders you asked, the FileSpec should look like:

    {
    "files": [{
        "aql": {
            "items.find": {
                "repo": "repo1",
                "path": "com/domain/name",
                "created": {
                     "$before": "3mo"
                },
                "type":"folder",
                "name": {"$match":"20*"}
            }
        }
    }]}
    

    A different approach for keeping your Artifactory clean from old build artifacts, is to use build-retention. This operation deletes old builds, and has an option to remove also the artifacts associated with the deleted build. You can read about discarding old builds in JFrog CLI documentation page. A discard command with JFrog CLI would be like:

    jfrog rt bdi <BUILD-NAME> --max-days 90 --delete-artifacts true
    

    Other than using JFrog CLI for deleting artifacts and builds, Artifactory Pro supports User Plugins. You either can write your own or edit an existing plugin to perform many operations in Artifactory.

    In the User-plugins GitHub repository, you can find examples for many useful plugins, such as this plugin for cleaning artifacts. As explained in the plugin page, the clean process can be set to run automatically as a scheduled job.