From 94a12b8347184eb0713c96d99661a1464b9d56a3 Mon Sep 17 00:00:00 2001 From: Bruno Gale Date: Sat, 21 Nov 2026 10:56:55 +0100 Subject: [PATCH] feat(storage): purge the CDN cache of the whole project --- .../Buckets/StorageBucketAnonTests.cs | 7 +++++++ .../Buckets/StorageBucketApiContractTests.cs | 19 +++++++++++++++++++ .../Files/PurgeCacheCancellationTests.cs | 9 +++++++++ .../Storage/Interfaces/IStorageBucketApi.cs | 10 ++++++++++ .../Storage/Storage/PublicAPI.Unshipped.txt | 2 ++ packages/Storage/Storage/PurgeCacheOptions.cs | 11 ++++++----- packages/Storage/Storage/StorageBucketApi.cs | 5 +++++ 7 files changed, 58 insertions(+), 5 deletions(-) diff --git a/packages/Storage/Storage.Tests/Buckets/StorageBucketAnonTests.cs b/packages/Storage/Storage.Tests/Buckets/StorageBucketAnonTests.cs index c763775a..133df303 100644 --- a/packages/Storage/Storage.Tests/Buckets/StorageBucketAnonTests.cs +++ b/packages/Storage/Storage.Tests/Buckets/StorageBucketAnonTests.cs @@ -62,4 +62,11 @@ public async Task DeleteBucket_ShouldThrow_GivenNoPermission() var act = () => this.PublicStorage.DeleteBucket(id); await act.Should().ThrowAsync(); } + + [TestMethod] + public async Task PurgeProjectCacheAsync_ShouldThrow_GivenNoPermission() + { + var act = () => this.PublicStorage.PurgeProjectCacheAsync(); + (await act.Should().ThrowAsync()).Which.StatusCode.Should().Be(403); + } } diff --git a/packages/Storage/Storage.Tests/Buckets/StorageBucketApiContractTests.cs b/packages/Storage/Storage.Tests/Buckets/StorageBucketApiContractTests.cs index 92d5dbc5..8736a290 100644 --- a/packages/Storage/Storage.Tests/Buckets/StorageBucketApiContractTests.cs +++ b/packages/Storage/Storage.Tests/Buckets/StorageBucketApiContractTests.cs @@ -240,6 +240,25 @@ public async Task PurgeBucketCache_ShouldEncodeDelimiters_GivenABucketIdWithUrlD "a raw '?' in the id would start a query string and purge the wrong bucket"); } + [TestMethod] + public async Task PurgeProjectCacheAsync_ShouldDeleteTheCdnRoot() + { + this.Respond("/storage/v1/cdn/", "DELETE", 200, "{\"message\":\"success\"}"); + await this.client.PurgeProjectCacheAsync(); + var request = this.SingleRequest(); + request.Method.Should().Be("DELETE"); + request.Path.Should().Be("/storage/v1/cdn/"); + request.Query.Should().BeNullOrEmpty("unset options must purge every cached version, not only the transformations"); + } + + [TestMethod] + public async Task PurgeProjectCacheAsync_ShouldRequestTransformationsOnly_GivenTheTransformationsOption() + { + this.Respond("/storage/v1/cdn/", "DELETE", 200, "{\"message\":\"success\"}"); + await this.client.PurgeProjectCacheAsync(new PurgeCacheOptions { Transformations = true }); + this.SingleRequest().Query.Should().Contain(pair => pair.Key == "transformations" && pair.Value.Contains("true")); + } + private void Respond(string path, string method, int statusCode, string body) => this.server.Given(Request.Create().WithPath(path).UsingMethod(method)) .RespondWith(Response.Create().WithStatusCode(statusCode) diff --git a/packages/Storage/Storage.Tests/Files/PurgeCacheCancellationTests.cs b/packages/Storage/Storage.Tests/Files/PurgeCacheCancellationTests.cs index 5de6d202..938307ba 100644 --- a/packages/Storage/Storage.Tests/Files/PurgeCacheCancellationTests.cs +++ b/packages/Storage/Storage.Tests/Files/PurgeCacheCancellationTests.cs @@ -46,6 +46,15 @@ public async Task PurgeBucketCache_ShouldForwardTheTokenToTheRequest() "the bucket purge must carry the caller's token so an in-flight request can be cancelled"); } + [TestMethod] + public async Task PurgeProjectCacheAsync_ShouldForwardTheTokenToTheRequest() + { + using var cts = new CancellationTokenSource(); + var act = () => this.client.PurgeProjectCacheAsync(options: null, cts.Token); + (await this.TokenSeenBy(act, cts)).Should().BeTrue( + "the project purge must carry the caller's token so an in-flight request can be cancelled"); + } + private async Task TokenSeenBy(Func act, CancellationTokenSource cts) { var requestSawLiveToken = false; diff --git a/packages/Storage/Storage/Interfaces/IStorageBucketApi.cs b/packages/Storage/Storage/Interfaces/IStorageBucketApi.cs index e87e5321..c60e9cb9 100644 --- a/packages/Storage/Storage/Interfaces/IStorageBucketApi.cs +++ b/packages/Storage/Storage/Interfaces/IStorageBucketApi.cs @@ -34,5 +34,15 @@ public interface IStorageBucketApi : IGettableHeaders /// /// Task PurgeBucketCache(string id, PurgeCacheOptions? options = null, CancellationToken cancellationToken = default); + + /// + /// Purges the CDN cache for every object in every bucket of the project. Requires a service-role key. + /// + /// + /// + /// await storage.PurgeProjectCacheAsync(); + /// + /// + Task PurgeProjectCacheAsync(PurgeCacheOptions? options = null, CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/packages/Storage/Storage/PublicAPI.Unshipped.txt b/packages/Storage/Storage/PublicAPI.Unshipped.txt index 7dc5c581..81587c7f 100644 --- a/packages/Storage/Storage/PublicAPI.Unshipped.txt +++ b/packages/Storage/Storage/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ #nullable enable +Supabase.Storage.Interfaces.IStorageBucketApi.PurgeProjectCacheAsync(Supabase.Storage.PurgeCacheOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! +Supabase.Storage.StorageBucketApi.PurgeProjectCacheAsync(Supabase.Storage.PurgeCacheOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! diff --git a/packages/Storage/Storage/PurgeCacheOptions.cs b/packages/Storage/Storage/PurgeCacheOptions.cs index 2eb084e3..c2a4cc23 100644 --- a/packages/Storage/Storage/PurgeCacheOptions.cs +++ b/packages/Storage/Storage/PurgeCacheOptions.cs @@ -1,15 +1,16 @@ namespace Supabase.Storage { /// - /// Options for and - /// . + /// Options for , + /// and + /// . /// public class PurgeCacheOptions { /// - /// If true, purges only the transformations (resized/formatted variants) for the object - /// or bucket, leaving the original cached file intact. If left null, all cached versions - /// are purged. + /// If true, purges only the transformations (resized/formatted variants) for the object, + /// bucket or project, leaving the original cached file intact. If left null, all cached + /// versions are purged. /// public bool? Transformations { get; set; } } diff --git a/packages/Storage/Storage/StorageBucketApi.cs b/packages/Storage/Storage/StorageBucketApi.cs index 42931dcc..2fe86360 100644 --- a/packages/Storage/Storage/StorageBucketApi.cs +++ b/packages/Storage/Storage/StorageBucketApi.cs @@ -179,4 +179,9 @@ public async Task CreateBucket(string id, BucketUpsertOptions? options = var url = options.ToPurgeUrl($"{this.Url}/cdn/{Helpers.EncodePath(id)}"); return Helpers.MakeRequestAsync(this.requestClient, this.Options, HttpMethod.Delete, url, null, this.Headers, cancellationToken); } + + /// + [ImplementedCapability("storage.file_buckets.purge_project_cache")] + public Task PurgeProjectCacheAsync(PurgeCacheOptions? options = null, CancellationToken cancellationToken = default) => + Helpers.MakeRequestAsync(this.requestClient, this.Options, HttpMethod.Delete, options.ToPurgeUrl($"{this.Url}/cdn/"), null, this.Headers, cancellationToken); }