Skip to content

feat(storage): add CDN cache purging for files and buckets - #1358

Merged
grdsdev merged 3 commits into
supabase:mainfrom
AndroidPoet:feat/storage-purge-cdn-cache
Sep 17, 2026
Merged

grdsdev merged 3 commits into
supabase:mainfrom
AndroidPoet:feat/storage-purge-cdn-cache

Conversation

@AndroidPoet

@AndroidPoet AndroidPoet commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Adds purgeCache(path:transformationsOnly:) on a bucket's file API and purgeCache(bucket:transformationsOnly:) on the Storage client, over Storage's DELETE /cdn/{bucket}/{path} and DELETE /cdn/{bucket} endpoints. After replacing a file at the same path, a backend can drop the stale CDN copy instead of waiting out cacheControl or adding a cache-busting parameter to every URL.

try await supabase.storage.from("avatars").purgeCache(path: "user123.png")
try await supabase.storage.purgeCache(bucket: "avatars", transformationsOnly: true)

Marks storage.file_buckets.purge_cache and storage.file_buckets.purge_bucket_cache implemented in sdk-compliance.yaml. The JS and Flutter clients already ship both.

Shape

  • Both return nothing, like emptyBucket and deleteBucket. Storage answers {"message":"success"}, which carries nothing a caller can act on.
  • transformationsOnly: becomes ?transformations=true, sent only when true. JS and Flutter name it transformations, but transformations: true reads as "purge the variants too", when it actually purges only the variants and leaves the original cached.
  • The file path goes through the same _getFinalPath and appendingPathComponent as info and exists, so a leading / is stripped and the key is percent-encoded. The JS client first shipped this without encoding and fixed it afterwards (fix(storage): url-encode object key in CDN purge methods supabase-js#2545); purgeCachePercentEncodesThePath pins it here.
  • The bucket id is interpolated as-is, like every other bucket route in this client.

Worth knowing

  • Storage serves these routes only to the service role, and only when a CDN purge endpoint is configured (CDN_PURGE_ENDPOINT_URL, plus the purgeCache tenant feature on multi-tenant deployments). The doc comments say so.
  • The bucket route and the transformations flag arrived in Storage v1.61.0 (feat: add ability to purge cdn for whole buckets, tenant, and transformations storage#1133). On an older self-hosted Storage, purgeCache(bucket:) gets a route-not-found 404, and purgeCache(path:transformationsOnly: true) purges the whole file, because the older file route ignores the query.

Testing

Five request-snapshot tests: a file, a file with transformationsOnly, a file path that needs percent-encoding, a bucket, and a bucket with transformationsOnly. Dropping the query item makes both transformationsOnly tests fail.

I also ran all four calls through the SDK with a throwaway test (not part of this PR) against Storage v1.77.4 on the local stack, with CDN_PURGE_ENDPOINT_URL pointed at a stub that logs what Storage forwards. That run predates the rename to purgeCache(bucket:), which only changes the call site: the request snapshots are the same.

POST /purge {"type":"object","tenant":{"ref":"stub"},"bucketId":"avatars","objectName":"folder/my file.png"}
POST /purge {"type":"object-transforms","tenant":{"ref":"stub"},"bucketId":"avatars","objectName":"folder/my file.png"}
POST /purge {"type":"bucket","tenant":{"ref":"stub"},"bucketId":"avatars"}
POST /purge {"type":"bucket-transforms","tenant":{"ref":"stub"},"bucketId":"avatars"}

The two file calls passed folder/my file.png and /folder/my file.png, and both reached the purge endpoint as the same object name. The bucket call with the anon key threw StorageError with kind .server, status 403 and Access denied: Invalid role.

The capability matrix checks from validate-sdk-compliance-swift.yml (pinned capability-matrix-v1.6.0) pass locally against main, with symbol graphs dumped and normalized the way CI does it.

$ swift test --skip IntegrationTests   # exit 0
9 test runs, all passed: 1416 tests in 141 suites, 174 of them in StorageTests

$ swift-format lint --recursive --strict Sources Tests   # exit 0
$ ./scripts/spell-check.sh   # exit 0, 413 files checked, 0 issues
$ ./scripts/test-docs.sh   # its docbuild and warning filter: exit 0, no warnings

Review first

The transformationsOnly: label, which departs from the JS and Flutter spelling on purpose.

Adds `StorageFileApi.purgeCache(path:transformationsOnly:)` and
`SupabaseStorageClient.purgeBucketCache(_:transformationsOnly:)`, covering
`storage.file_buckets.purge_cache` and `storage.file_buckets.purge_bucket_cache`.
@AndroidPoet
AndroidPoet requested review from a team and grdsdev as code owners September 16, 2026 18:52
@coderabbitai

coderabbitai Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: ec7adeec-15c9-499f-b140-39b9e7b5a0ec

📥 Commits

Reviewing files that changed from the base of the PR and between dcbbee3 and 97f5ee1.

📒 Files selected for processing (6)
  • Sources/Storage/StorageFileApi.swift
  • Sources/Storage/SupabaseStorage.swift
  • Sources/Storage/SupabaseStorageClient+Buckets.swift
  • Tests/StorageTests/StorageBucketAPITests.swift
  • Tests/StorageTests/StorageFileAPITests.swift
  • sdk-compliance.yaml
🚧 Files skipped from review as they are similar to previous changes (1)
  • Sources/Storage/SupabaseStorage.swift

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Summary

Summary by CodeRabbit

  • New Features

    • Added options to purge the cache for an entire storage bucket or an individual file.
    • Added support for purging only transformed file variants.
    • File paths, including spaces, are safely encoded in purge requests.
  • Documentation

    • Updated storage API documentation to include cache management operations.

Walkthrough

Storage adds file-level and bucket-level cache purge APIs. Both APIs send DELETE requests to CDN cache endpoints. When transformationsOnly is true, each request includes transformations=true. File-level requests preserve percent-encoded paths. Tests cover standard requests, transformation-only requests, and file paths containing spaces. SDK compliance entries now mark both capabilities as implemented.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant StorageSDK
  participant CDNCacheEndpoint
  Caller->>StorageSDK: Request file or bucket cache purge
  StorageSDK->>CDNCacheEndpoint: DELETE cache endpoint
  CDNCacheEndpoint-->>StorageSDK: Success response
  StorageSDK-->>Caller: Completion
Loading

Priority: ⬇️ Low

Change: Feature

Merge Risk: ⚪ Minimal · up to 97f5e

The cache purge APIs match the documented endpoints and are covered for bucket, file, query, and encoded-path requests.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@grdsdev grdsdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Per the Swift API Design Guidelines (clarity at the point of use / include all words needed to avoid ambiguity), the two new purge methods label their identifying argument inconsistently:

try await supabase.storage.from("avatars").purgeCache(path: "user123.png")
try await supabase.storage.purgeBucketCache("avatars", transformationsOnly: true)

purgeCache(path:) labels its string because an unlabeled string could mean anything (path, key, URL). purgeBucketCache(_:) does the same job — identifying the thing whose cache gets purged — but leaves it unlabeled. Read on its own, purgeBucketCache("avatars", ...) doesn't make it clear "avatars" is a bucket id, especially with Cache as the noun immediately preceding the argument slot (a reader could momentarily read it as identifying which cache, not which bucket).

Suggest labeling it to match the file-scoped method, and dropping "Bucket" from the base name since the label now carries that meaning (no collision — different receiving types):

public func purgeCache(bucket: String, transformationsOnly: Bool = false) async throws
storage.from("avatars").purgeCache(path: "user123.png")
storage.purgeCache(bucket: "avatars", transformationsOnly: true)

Both call sites now read the same way and disambiguate their argument the same way.

grdsdev added a commit that referenced this pull request Sep 17, 2026
Captures the naming/clarity rules from swift.org's API Design
Guidelines as a project skill, alongside swift-docc and
writing-migration-guides. Used to review #1358 and drive this PR.
@AndroidPoet

Copy link
Copy Markdown
Contributor Author

Renamed to purgeCache(bucket:transformationsOnly:) in 97f5ee1, along with its parameter doc, the SupabaseStorageClient topics entry, the two bucket tests and the sdk-compliance.yaml symbol. I also merged main so it builds on top of #1355. The description now uses the new name.

@grdsdev
grdsdev enabled auto-merge (squash) September 17, 2026 17:00
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 35249473153

Coverage increased (+0.02%) to 89.429%

Details

  • Coverage increased (+0.02%) from the base build.
  • Patch coverage: 18 of 18 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 11976
Covered Lines: 10710
Line Coverage: 89.43%
Coverage Strength: 896736.98 hits per line

💛 - Coveralls

@grdsdev
grdsdev merged commit 6e069a6 into supabase:main Sep 17, 2026
37 checks passed
grdsdev added a commit that referenced this pull request Sep 17, 2026
…delines (#1368)

* refactor(storage)!: align remaining API with the Swift API Design Guidelines

SortBy.order stored the raw "asc"/"desc" string even though its own
initializer already took a type-safe SortOrder, discarding that safety
one property read later. Type it SortOrder directly.

upload, update, and uploadToSignedURL left their file path unlabeled
while every other StorageFileApi method taking a path (download, info,
exists, createSignedURL, ...) labels it path:. Label it there too, for
the same reason the others do: an unlabeled String at the call site
doesn't tell a reader whether it's a path, a key, or a URL.

See V3_MIGRATION.md for call-site examples.

* chore: add swift-api-design-guidelines skill

Captures the naming/clarity rules from swift.org's API Design
Guidelines as a project skill, alongside swift-docc and
writing-migration-guides. Used to review #1358 and drive this PR.
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.

3 participants