2

I am trying to delete artifacts ( older than 6 months) from a list of repositories in jfrog artifactory. I am creating a spec file like below and using that spec i am deleting using jfrog cli. My query is there any way i can execute the aql in loops instead of manually updating the repo name: foobar

 {
  "files": [
    {
      "aql": {
        "items.find": {
          "repo": "foobar",
          "$or": [
            {
              "$and": [
                {
                  "modified": { "$lt": "2021-06-06T21:26:52.000Z"}
                }
              ]
            }
          ]
        }
      }
    }
  ]
}```
jfrog rt del --spec /tmp/foo.spec --dry-run

I want to run the aql in loops only change will be the repo name . Is there a way to do it ?
sg0133
  • 69
  • 7

3 Answers3

4

Looks we don't have direct way to achieve this, but you can make use of -spec-vars in order to pass dynamic variables through CLI command. I meant to say we can write a script to pass names of selected repositories in a loop and use --spec-vars as below:

jfrog rt del --spec test.spec --spec-vars "RepoKey=libs-release-local" --dry-run

and spec file will be looks below:

{
  "files": [
    {
      "aql": {
        "items.find": {
          "repo": "${RepoKey}",
           ...............
        }
      }
    }
  ]
}

Also, there is a Artifactory Cleanup User Plugin where we can specify required repositories names to cleanup as per your needs, hence, you may refer this too.

sankar dunga
  • 346
  • 1
  • 3
1

You may also want to consider using the JFrog-CLI plugin: https://github.com/jfrog/jfrog-cli-plugins/tree/main/rt-cleanup Which is a bit more easy than the file-spec approach. All you need to do is to install the plugin:

jf plugin install rt-cleanup

and then delete the old artifacts:

jf rt-cleanup clean example-repo-local --time-unit=month --no-dl=6
Talar
  • 21
  • 3
0

We've released the tool that can help you to avoid bug ugly AQL quires for your cleanup policy. https://github.com/devopshq/artifactory-cleanup

The recent release support YAML configuration, for your case it looks like this:

# artifactory-cleanup.yaml
artifactory-cleanup:
  server: https://repo.example.com/artifactory
  # $VAR is auto populated from environment variables
  user: $ARTIFACTORY_USERNAME
  password: $ARTIFACTORY_PASSWORD

  policies:
    - name: Remove all files from repo-name-here older than 180 days
      rules:
        - rule: Repo
          name: "reponame"
        - rule: DeleteOlderThan
          days: 180

then you can run it in "dry run" mode (which is the mode by default) to see all files that the tool is going to delete:

# Set the credentials with delete permissions
export ARTIFACTORY_USERNAME=usernamehere
export ARTIFACTORY_PASSWORD=password

artifactory-cleanup

if you agreed - run it in destroy mode

artifactory-cleanup --destroy

to go over multiple repositories if they match some pattern - you can use RepoByMask rule instead of Repo.

The below example will remove all files older than 180 days from myrepo.snapshot, otherrepo.snapshot and other that match the mask

- rule: RepoByMask
  mask: "*.snapshot"