Search code examples
artifactoryartifactory-query-lang

Delete artifacts in artifactory under specified path based on how old they are


I have a need to delete the artifact versions for one of my repos in artifactory that are older than certain number of days. By this i mean that suppose my artifactory url repo is :-

https://artifactory.mycompany.com/artifactory/myrepo/

and under that repo there are few folders with the name pattern abc-*-xyz, which means the fully qualified paths here for those folders would be :-

https://artifactory.mycompany.com/artifactory/myrepo/abc-1-xyz
https://artifactory.mycompany.com/artifactory/myrepo/abc-2-xyz
https://artifactory.mycompany.com/artifactory/myrepo/abc-3-xyz

Now under each of these folders are stored the actual versioned artifact folders that i would like to only delete, which means that entire version folder for example for two of the urls that would be :-

https://artifactory.mycompany.com/artifactory/myrepo/abc-1-xyz/ver_11
https://artifactory.mycompany.com/artifactory/myrepo/abc-1-xyz/ver_12
https://artifactory.mycompany.com/artifactory/myrepo/abc-2-xyz/ver_3
https://artifactory.mycompany.com/artifactory/myrepo/abc-2-xyz/ver_5
So based on say if these folders ver_* are older than 30 days, i would like to get them deleted. And only these ver_* folders not abc-*-xyz, etc

I could see the below stackoverflow question and able to get that working for that use case but unable to construct the AQL for my use case so that i can have the spec file accordingly that can be used to delete what i need here.

Artifactory delete all artifacts older than 6 months

Any help here to help me get the AQL file constructed and then the spec file that can be called for actual deletion, would be greatly appreciated as always.


Solution

  • The following file spec does what you're looking for:

    {
      "files": [
        {
          "aql": {
            "items.find": {
              "repo": "myrepo",
              "path": {"$match":"abc-*-xyz"},
              "name": {"$match":"ver_*"},
              "type": "folder",
              "$or": [
                {
                  "$and": [
                    {
                      "created": { "$before":"7d" }
                    }
                  ]
                }
              ]
            }
          }
        }
      ]
    }
    

    The above file spec finds all the folders which match the following criteria:

    1. They are under the my-repo repository.
    2. They are inside a folder with a name that matches abc-*-xyz and is located at the root of the repository.
    3. Their name matches ver_*
    4. They were created more that 7 days ago.

    To delete all the folders found by this file spec, do the following.

    1. Create the file spec and name it as you like. Let's call it delete-folders-spec for the sake of this example.
    2. From your terminal, run the following JFrog CLI command, while inside the same directory as delete-folders-spec

    jfrog rt del --spec delete-folders-spec