Project

Completed

JFrog Log Purge Tool

A scheduled Cloud Run Job that clears year-old logs out of JFrog Artifactory, and the three deployment designs it took to get there.

This is another work project, a companion to the JFrog Artifact Scanner. As before I can’t share the source code, but I can share the problem, the design and what I learned. This one taught me less about JFrog and more about choosing how to deploy something small.

The problem

JFrog Artifactory keeps its own system and request logs in a built-in repository called jfrog-logs. Artifactory writes to it constantly, and nothing ever removes anything from it.

So it just grows. Old logs sit next to yesterday’s, counting towards storage, and nobody will ever read them. There are no mechanisms to create a clean up retention policy which is surprising as this is something one becomes used to using as a matter of lifecycle policy with vendors.

Cleaning it up manually meant someone deleting old files by hand now and then, which is a real pain as every 2-hours there’s a new archive file, and each file needs to be selected and deleted. Imagine doing this for years worth of logs? Who’s going to do it?

As a result of the JFrog Artifact Scanner I created, I investigated if a function could be created to delete old logs. I created a proof-of-concept (POC) script which I ran manually, it hit the API and deleted old logs. Once I showed it to my team I got the go ahead to automate it.

The idea

Delete anything older than a year, once a week at 02:00 on Monday, and keep each run small enough to be safe:

  1. Find files in jfrog-logs older than the retention period, oldest first
  2. Delete a capped batch of them
  3. Leave whatever’s left for the next run
  4. Record what happened

The first few runs work through whatever backlog has built up. After that, each run only has a week’s worth of newly expired logs.

Architecture

Log purge tool cloud architecture

Like the scanner, the purge tool is a containerised Python app running as a Cloud Run Job. It’s a batch task with a clear start and end. It doesn’t need to listen for requests, and a failed run should show up as a failed execution, which is exactly how Cloud Run Jobs behave.

It reuses the scanner’s layout:

  • ci-project holds the build side. Cloud Build builds the image when changes merge to main, and Artifact Registry stores it.
  • runtime-project holds the runtime side: Cloud Scheduler, the Cloud Run Job and Secret Manager. It’s the same project the scanner runs in.

The job pulls its image from the CI project’s registry across the project boundary, so the runtime side needs an IAM binding on that registry, just as the scanner does. Outbound traffic goes through the project’s serverless VPC connector to reach Artifactory.

Getting to this architecture took three attempts. More on that below.

How a run works

Log purge tool run flow

1. Fail fast

The job reads all its settings at start-up: the JFrog URL, the token, the repository name, the retention period and the batch limits. If any of them is missing, it fails immediately. It then pings Artifactory before touching any data. Either failure exits non-zero, and Cloud Run marks the execution as failed.

2. Find the oldest logs

One AQL (Artifactory Query Language) query does all the searching:

items.find({
  "repo": {"$eq": "jfrog-logs"},
  "type": {"$eq": "file"},
  "created": {"$before": "365d"}
})
.include("repo", "path", "name", "created", "size")
.sort({"$asc": ["created"]})
.limit(10000)

It asks for files only, created more than 365 days ago, oldest first, with a cap of 10,000 per run. It also returns each file’s size, so the job can report how much space it freed.

There’s no pagination, deliberately. Files deleted in one run don’t come back in the next run’s query, so each run naturally carries on from where the last one stopped. There’s no offset to track and no state to keep between runs.

3. Delete in parallel

Each file is deleted with its own call to the Artifactory delete API. As with the scanner’s status checks, the deletes run in a thread pool of 10 workers sharing one HTTP session, with retries and exponential backoff on 429 and 5xx responses.

A 404 counts as success. If a file is already gone, whether from an earlier retried run or someone deleting it by hand, that’s the outcome we wanted anyway. That makes every run safe to repeat.

4. Stay inside a time budget

Cloud Run Jobs have a task timeout, and a job killed halfway through doesn’t get to report what it did. So the job has its own, shorter budget: after 25 minutes it stops starting new deletes, finishes the ones already in flight, and counts the rest as skipped. The skipped files are simply the oldest ones left for the next run. In reality when the function first ran it found just over 7000 files to delete, and executed deletions in about 30-seconds. 7000 files, 30-seconds. Definitely a case for automation right there!

5. Report

Every run ends with one structured PURGE_SUMMARY log entry:

{
  "found": 10000,
  "deleted": 9998,
  "failed": 2,
  "skipped": 0,
  "bytes_freed": 5368709120,
  "backlog_remaining": true,
  "duration_seconds": 412.3
}

backlog_remaining is true when the run hit its cap or its time budget, which tells you more old logs are still waiting. If any delete failed, the job exits non-zero, so the problem shows up in Cloud Run and doesn’t stay hidden in the logs.

The same summary goes to Slack: live or dry run, files found, files deleted, failures, space freed, and whether a backlog remains. Like the scanner, it publishes to a shared Pub/Sub topic in a monitoring project that a Slack-routing service already listens to, so there are no webhooks or Slack credentials in the job. The runtime project sits inside a VPC Service Controls perimeter, so the job’s service account also had to be added to the perimeter’s Pub/Sub egress rule, next to the scanner’s.

Design decisions

Dry run by default

Deleting data is a different level of risk from re-scanning it, so DRY_RUN defaults to true, and the tool runs that way first. In dry-run mode it does the search and reports everything it would delete, printing the first 10 paths and counting the rest, without deleting anything. Once the dry-run numbers had been reviewed, switching to live deletes was a one-line change, and it’s now running live.

Oldest first, capped, and stateless

Capping each run and sorting oldest first makes the backlog work itself down without any bookkeeping. A big first run can’t run away, a failed run loses nothing, and the next run carries on. No state is kept anywhere.

Configuration lives with the code

This is where it differs from the scanner. The scanner’s settings live in Terraform, because they change on their own: adding a repository to scan doesn’t need new code. The purge tool’s settings don’t change on their own like that, so they’re ENV lines in the Dockerfile:

ENV JFROG_LOG_REPO=jfrog-logs
ENV DRY_RUN=false
ENV RETENTION_DAYS=365
ENV MAX_ITEMS_PER_RUN=10000
ENV RUN_BUDGET_SECONDS=1500

I learned the hard way that splitting them was a problem. When code and config were in different repos, adding a setting meant one PR to Terraform, applied first, then a second PR to the code, in that order. Get the order wrong and the new code failed to start because its setting wasn’t there yet. With both in the Dockerfile, one PR changes both, and they always ship together.

The only setting Terraform still puts on the job is a reference to the token’s secret.

Secrets fetched at runtime

The JFrog admin token lives in Secret Manager. Cloud Run injects it into the job as an environment variable when it starts. Terraform only names the secret, so the token’s value is never in the image, the Dockerfile or Terraform. It’s the same token the scanner uses, so there’s one token to rotate on its 90-day cycle, not two.

Getting to boring

Three deployment designs for the log purge tool

The purge logic was the easy part. Deciding how to deploy a script this small took three attempts.

1. A Cloud Function. It’s one Python function run on a schedule, so a gen2 Cloud Function seemed natural. But the code and the infrastructure live in different repos, so Terraform couldn’t zip the source itself. That meant CI building a zip into a bucket, a third infrastructure repo for the build trigger and bucket, and Terraform redeploying from the latest zip. That was a lot of moving parts for one script.

2. A Cloud Run service built with buildpacks. Next I tried for the fewest pieces: two repos, no Dockerfile. Google’s buildpacks can turn a folder with requirements.txt and a Procfile straight into an image, and gcloud run deploy --source does the whole build and deploy. Terraform owned the service’s config and ignored its image, and gcloud owned the image. It worked, but it had catches:

  • Terraform created the service with Google’s placeholder hello image. My first scheduled “run” returned HTTP 200 and did nothing, because I hadn’t yet deployed any code. The logs said Hello from Cloud Run!, which was a nice greeting but not what I was after.
  • A service needs an HTTP entry point, so a batch job had to pretend to be a web server.
  • Automating the deploy was where it really fell apart. A Cloud Build trigger in the CI project running gcloud run deploy --source against the runtime project actually starts a second build in the runtime project. That’s two builds in two projects, each running as a different service account, and the cross-project IAM to make it work took a diagram just to explain.

3. A Cloud Run Job, built like the scanner. In the end I stepped back. The scanner already solves this exact shape of problem: a Dockerfile, a Cloud Build trigger in the CI project, a registry the runtime project pulls from, and a Cloud Run Job started by Cloud Scheduler. Its PRs had been reviewed, its cross-project IAM was already in place, and the rest of the team already understood it. Following the same pattern meant most of the infrastructure was a copy with the names changed.

Option 2 was the most technically interesting, and option 3 is the one I’d want to be on call for.

What I learned

  • Consistency beats cleverness for small tools. Matching an existing, understood pattern was worth more than saving a Dockerfile or a repo. Reviewers, and future me, only need to learn one shape.
  • Count the IAM, not the repos. “Fewer repos” sounded simpler, but where the build runs and which identity deploys the code mattered far more. Two builds in two projects isn’t simpler, even if it’s fewer files.
  • Keep config that changes with the code next to the code. If a setting can only change alongside a code change, splitting the two across repos just creates an ordering problem.
  • Know what’s actually running. A placeholder image that returns 200 looks just like a healthy service from the scheduler’s side. Check the image, not just the status code.
  • Design deletes to be repeated. Oldest first, capped, stateless, 404 counts as done: every run can safely be re-run, which is what you want from anything that deletes data.

What’s next

  • Bring the scanner in line. The scanner still keeps its settings in Terraform. Moving them into its Dockerfile, as here, would mean both tools are configured the same way, and support only has one place to look.
  • Make sure new builds are picked up. Cloud Run may pin a job to a specific image digest when the job is created or updated. I want to confirm a newly pushed :latest actually reaches the job, and if it doesn’t, add a step to the build that updates the job.

Now that it’s live, the jfrog-logs repository stops growing beyond a year’s worth of logs, with no further effort from anyone, and a Slack message every Monday shows it’s still doing its job.