Skip to content

PREQ-5729: fix argument list too long in cache cleanup#61

Merged
mikolaj-matuszny-ext-sonarsource merged 2 commits into
masterfrom
PREQ-5729-fix-argument-list-too-long
May 11, 2026
Merged

PREQ-5729: fix argument list too long in cache cleanup#61
mikolaj-matuszny-ext-sonarsource merged 2 commits into
masterfrom
PREQ-5729-fix-argument-list-too-long

Conversation

@mikolaj-matuszny-ext-sonarsource
Copy link
Copy Markdown
Contributor

Summary

  • Fixes Argument list too long error when running cache cleanup on repos with large caches (e.g. sonar-enterprise with 1 TB+ cache)
  • Writes the delete JSON payload to a temp file and passes it via file:// URI instead of inline shell argument, avoiding OS ARG_MAX limits

Root cause

aws s3api delete-objects --delete "$DELETE_JSON" passes the entire JSON blob as a process argument. When S3 keys are long and there are up to 1000 per batch, this exceeds the kernel argument size limit (~2 MB on Linux).

Fix

Use file:// as documented by AWS CLI:

aws s3api delete-objects --bucket "$BUCKET" --delete "file://$DELETE_FILE"

Test plan

  • Run cache cleanup on sonar-enterprise master branch (the failing case from PREQ-5729)
  • Verify cleanup completes without Argument list too long error

Pass delete JSON via file:// instead of inline argument to avoid
exceeding ARG_MAX on repos with large caches (e.g. 1 TB+).
@mikolaj-matuszny-ext-sonarsource mikolaj-matuszny-ext-sonarsource requested a review from a team as a code owner May 11, 2026 13:42
@sonar-review-alpha
Copy link
Copy Markdown

sonar-review-alpha Bot commented May 11, 2026

Summary

This PR fixes the Argument list too long error in cache cleanup by writing the AWS delete JSON payload to a temporary file instead of passing it as a command-line argument. This bypasses OS ARG_MAX limits (~2 MB) when deleting large batches of S3 objects.

What changed:

  • JSON blob is now written to a temp file and passed via file://$DELETE_FILE URI (AWS CLI standard)
  • DELETE_JSON variable was replaced with a temporary file created per batch
  • EXIT trap updated to clean up both the keys file and delete file
  • Each batch deletes its temp file immediately after use for cleanup

This is a minimal, mechanical change that preserves all existing logic—only the mechanism for passing the JSON changes.

What reviewers should know

Start here: Look at the aws s3api delete-objects invocation (lines ~233-234) to see the core change: --delete "file://$DELETE_FILE" instead of --delete "$DELETE_JSON".

Key implementation details:

  • DELETE_FILE="" is initialized before the loop because it's referenced in the EXIT trap and may not exist on first trap execution
  • The trap uses "${DELETE_FILE:-}" to safely expand an empty variable (standard shell idiom)
  • Each batch iteration creates a fresh temp file, writes to it, uses it, then deletes it—no temp files are left behind
  • The jq output now goes directly to file with > "$DELETE_FILE" rather than into a variable

Why this works: The AWS CLI's file:// URI scheme allows passing large payloads without shell argument limits. This is documented behavior, not a workaround.

Testing note: The fix enables cleanup on repos with huge caches (1 TB+) where the argument size was previously exceeded.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

sonar-review-alpha[bot]

This comment was marked as resolved.

Extend the EXIT trap to cover DELETE_FILE so it gets cleaned up
even if jq or another command fails mid-loop under set -e.
Reset DELETE_FILE after each successful rm to avoid stale paths.
@sonarqube-cloud-us
Copy link
Copy Markdown

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

🗣️ Give feedback

Comment thread scripts/cleanup-cache.sh
Comment thread scripts/cleanup-cache.sh
Comment thread scripts/cleanup-cache.sh
@mikolaj-matuszny-ext-sonarsource mikolaj-matuszny-ext-sonarsource merged commit 60d0720 into master May 11, 2026
23 checks passed
@mikolaj-matuszny-ext-sonarsource mikolaj-matuszny-ext-sonarsource deleted the PREQ-5729-fix-argument-list-too-long branch May 11, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants