Skip to content

api: delete users in the background - #114

Merged
thecodrr merged 3 commits into
masterfrom
feat/background-user-deletion
Oct 29, 2026
Merged

thecodrr merged 3 commits into
masterfrom
feat/background-user-deletion

Conversation

@thecodrr

@thecodrr thecodrr commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Deleting a large account timed out: every collection's delete ran in one Mongo transaction, which outlived Mongo's 60s limit and the request timeout.

Changes

  • POST /users/delete marks the user's user_settings with DeletedAt (upserted), unregisters the user from Identity and returns. Same endpoint, form and response, so old clients are unaffected.
  • UserPurgeJob (every 5 min) purges marked users after a 150 min grace period, covering uploads a token cached at deletion (30 min) can still start: a 5 GiB direct upload (90 min) or a presigned upload URL (1 h). Each step is idempotent and non-transactional, and user_settings is deleted last, so a failed purge is retried. A user that still exists in Identity is unmarked instead of purged.
  • SyncAuthorizationHubFilter cuts off a deleted user's open sync connections (hub connections outlive their token) and takes over the sync access check from SyncV2Hub.OnConnectedAsync.
  • Identity: UnregisterUserAsync ends sessions before deleting, throws on failure or for another client's user, and clears the API's cached introspection of the ended tokens. user_exists is added; delete_user stays for API instances that predate this change, to be removed in a later release.

Deploy order

Identity first (user_exists, unregister_user; old APIs keep working through delete_user), then this API.

Testing

UserPurgeJobTests, SyncV2HubTests, ApiErrorResponseTests, TokenContractTests.

Deleting a large account ran every collection's delete in one Mongo
transaction, which outlived Mongo's time limit and the request timeout.

The delete now marks the user's settings with DeletedAt and unregisters
the user from Identity, and UserPurgeJob purges the data after a grace
period, one idempotent step at a time, retrying until it's done. A user
that still exists in Identity is unmarked instead of purged.

A deleted user's open sync connections are cut off by a hub filter, which
also takes over the sync access check, and unregistering clears the
user's cached token introspection.

Identity drops delete_user and adds user_exists, so it must be deployed
before this API.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved issues can permanently prevent data purging, allow late uploads to recreate deleted data, and break account deletion during rollout, while existing-connection sync protection lacks test coverage.

4 open issues
What changed in this PR

Moves account data deletion out of the request’s MongoDB transaction into a background purge job, reducing timeouts for large accounts while preserving the deletion endpoint.

Changes:

  • Marks accounts for deletion and schedules non-transactional cleanup after a grace period.
  • Updates Identity unregistration to end sessions and invalidate cached tokens.
  • Adds sync authorization filtering and tests for purge behavior and deleted-user access.
File Description
tests/​Streetwriters.Identity.Tests/​TokenContractTests.cs Updates unregistration expectations and removes the old deletion test.
tests/​Notesnook.API.Tests/​UserPurgeJobTests.cs Tests purge isolation, grace periods, retries, and Identity failures.
tests/​Notesnook.API.Tests/​SyncV2HubTests.cs Tests connection authorization and deleted-user rejection.
tests/​Notesnook.API.Tests/​ApiErrorResponseTests.cs Updates the deletion service stub.
Streetwriters.Identity/​Services/​UserAccountService.cs Adds existence checks and session-aware unregistration.
Streetwriters.Identity/​Services/​ScopedUserAccountService.cs Exposes the new existence check through scoped delegation.
Streetwriters.Identity/​Services/​PersistedGrantSessionStore.cs Returns ended-session keys for cache invalidation.
Streetwriters.Identity/​OpenIddictEngine/​OpenIddictSessionStore.cs Returns modern and legacy session cache keys.
Streetwriters.Identity/​Legacy/​LegacyGrants.cs Collects grant keys before deletion.
Streetwriters.Identity/​Interfaces/​ISessionStore.cs Updates the session termination return contract.
Streetwriters.Common/​Interfaces/​IUserAccountService.cs Replaces the deletion RPC with an existence check.
Notesnook.API/​Startup.cs Registers the sync filter and recurring purge job.
Notesnook.API/​Services/​UserService.cs Marks accounts before unregistering them from Identity.
Notesnook.API/​Services/​MongoIndexesHostedService.cs Indexes deletion timestamps for purge selection.
Notesnook.API/​Models/​UserSettings.cs Adds the deletion timestamp.
Notesnook.API/​Jobs/​UserPurgeJob.cs Implements delayed, retryable account data cleanup.
Notesnook.API/​Hubs/​SyncV2Hub.cs Moves connection authorization into the filter.
Notesnook.API/​Hubs/​SyncAuthorizationHubFilter.cs Checks deletion status on connections and method calls.
Notesnook.API/​Controllers/​UsersController.cs Removes the extended deletion request timeout.

Review effort: Balanced

Comment thread Notesnook.API/Jobs/UserPurgeJob.cs Outdated
Comment on lines +19 to +20
// longer than a cached token's introspection (30m) and a presigned upload URL (1h), so writes made with either land before the purge
public static readonly TimeSpan GracePeriod = TimeSpan.FromMinutes(75);
Comment on lines +52 to +54
await repositories.UsersSettings.Collection.UpdateOneAsync(
Builders<UserSettings>.Filter.Eq((s) => s.UserId, userId),
Builders<UserSettings>.Update.Unset((s) => s.DeletedAt));
Comment on lines +23 to +24
await EnsureNotDeletedAsync(invocationContext.Context, invocationContext.ServiceProvider);
return await next(invocationContext);
Comment on lines +11 to +12
[WampProcedure("co.streetwriters.identity.users.user_exists")]
Task<bool> UserExistsAsync(string userId);
Covers uploads admitted by a cached token in the grace period, keeps a mark set by a retried delete, tests that open connections are cut off, and keeps delete_user for API instances that predate this change.
@thecodrr
thecodrr merged commit 772c644 into master Oct 29, 2026
1 check passed
@thecodrr
thecodrr deleted the feat/background-user-deletion branch October 29, 2026 04:52
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