Search code examples
pythonartifactory

Python - Delete files from artifactory older than X hours


I'm looking to delete files older than 24 hours from a repository in the JFrog Artifactory.

Is there any python library available ready made ?

I can think of this way - os.system(curl -XDELETE https://my.artifactory.instance/artifactory/repo_to_search/test.txt) but is there more pythonic way to achieve it ?

I have limited access to the VM, where the script will run, so I can't install Jfrog CLI as well.


Solution

  • In order to use the JFrog CLI, it is not mandatory that it should be on the same VM. The JFrog CLI can be installed on the local machine and can be configured with the Artifactory. While using JFrog CLI, we can follow the below steps:

    1. Configure JFrog CLI with Artifactory.

    2. Create a Json(here aql.json) file having the AQL to find the artifacts based on the requirements. You can refer to our AQl documentation for more details on how to construct it. Below is a sample AQL for the mentioned requirement:

        "files": [
          {
            "aql": {
              "items.find": {
                "repo": "demo-pypi",
                "$or": [
                  {
                    "$and": [
                      {
                        "created": { "$before":"1d"}
                      }
                    ]
                  }
                ]
              }
            }
          }
        ]
      
      
    3. Run the below command to fetch the artifacts that are eligible for deletion:-

      jf rt s --spec aql.json

    4. Run the below command to finally delete the artifacts:-

      jf rt del --spec aql.json

    The above command will delete all the artifacts older than 1 day. Hope this helps.