Short answer: treat an avatar replacement as a tenant-scoped record change, not an overwrite. Write the accepted bytes to a new object key, move the database pointer only after that write succeeds, and retain the previous key until the product's rollback promise expires. The decision is about ownership and recovery first; storage cleanup comes later.

For a property-management product, this matters when a tenant manager replaces a building or user image and an export must contain only that tenant's files. A stable key looks tidy, but it makes the storage object carry too much meaning. The database should decide which immutable object is visible.

Policy Governance boundary Rollback Cleanup model Appropriate use
Overwrite a stable key Key and authorization must stay correct during every write None without another copy Immediate or ad hoc Disposable cache content
New key plus active pointer Tenant is recorded on each object and state transition Available during retention Scheduled, state-aware deletion Tenant-facing avatars
Native storage history Provider policy becomes part of the contract Depends on retained history and controls Provider-specific Compliance or archival requirements

Use the middle policy for tenant-facing avatars. It gives the application a clear owner, a visible current state, and a recovery window without claiming that object storage and the database commit atomically. I benchmark the critical path, but I don't use a single local upload as proof that cleanup or large-file behavior is healthy.

What should tenant isolation decide before an avatar upload?

The first decision is not the filename. It is who is allowed to create, activate, read, export, and delete the object. Derive tenantId from authenticated application context. Do not accept it as an authority from a browser field or trust a path merely because it contains a tenant-looking prefix.

A useful record keeps the boundary explicit:

Field Governance job
tenant_id Authorization and export scope
user_id Image subject
object_key Immutable candidate or active object
state Candidate, active, retained, or deleted
created_at Age checks and investigation
delete_after Explicit retention decision

The export query should filter by tenant in the database before it resolves object keys. The cleanup worker should do the same, then re-check that a retained object is no longer the active pointer. A matching string prefix is useful routing data. It is not an authorization system.

This is also where migration gets interesting. If an existing product has one stable avatar key per user, do not silently reinterpret that key as rollback history. Register the current object as the active record, then write future replacements under unique keys. During migration, measure untracked objects and keep a reconciliation report; deleting anything that merely looks old is how a cleanup job becomes a data-loss job.

That last point is easy to miss.

How can an avatar upload overwrite an old file in object storage without versioning?

Do not make the old file the write target when rollback is part of the product promise. Use a candidate key such as tenants/t_104/users/u_882/avatar/2026/08/11/7f3.... The identifier can be random. The tenant prefix should be unambiguous, and the key should be generated by the service after authorization.

The application state transition is deliberately boring:

  1. Validate type, size, tenant membership, and the upload's idempotency identity.
  2. Create a unique candidate key within the authorized tenant.
  3. Write the bytes and verify completion.
  4. Change the active pointer only after the write succeeds.
  5. Record the predecessor and its delete_after time.
  6. Let a separate worker remove the predecessor after retention expires.

An interrupted upload therefore leaves the previous pointer untouched. A successful object write followed by a failed pointer update leaves an orphan candidate that age-based reconciliation can inspect. Two concurrent uploads still need a defined winner, using a transaction, version column, or queue. The object name is not a lock.

Multipart uploads deserve their own test case. The public multipart overview documents an upload lifecycle that can span multiple parts, so the application should measure candidate age and incomplete-work cleanup under the same object sizes and concurrency it expects in production. Your mileage may vary when slow clients dominate the request time. That uncertainty belongs in the test plan, not in an optimistic timeout.

No magic transaction exists here.

What does a tenant-scoped rollback and old-key deletion contract contain?

The pointer is the application commit record. A rollback operation should select a retained object belonging to the same tenant, verify that it is still eligible, and activate it through the same concurrency rule as a normal replacement. Deleting the old key is a later state transition, not part of the request that makes the new avatar visible.

Here is the small boundary I want in a CLI or SDK: generic storage behavior on one side, tenant and pointer policy on the other. The adapter can use a provider SDK or an HTTP client; the state machine should not need to know which one.

type AvatarState = "candidate" | "active" | "retained" | "deleted"; type AvatarObject = { tenantId: string; userId: string; objectKey: string; state: AvatarState; deleteAfter: Date | null; }; interface ObjectStore { put(key: string, bytes: Uint8Array, contentType: string): Promise<void>; delete(key: string): Promise<void>; } interface AvatarRepository { getActive(tenantId: string, userId: string): Promise<AvatarObject | null>; activate( tenantId: string, userId: string, nextKey: string, predecessorKey: string | null, deleteAfter: Date | null, ): Promise<void>; } function avatarKey(tenantId: string, userId: string, uploadId: string): string { return `tenants/${tenantId}/users/${userId}/avatar/${uploadId}`; } async function replaceAvatar( store: ObjectStore, repository: AvatarRepository, tenantId: string, userId: string, uploadId: string, bytes: Uint8Array, contentType: string, retainUntil: Date | null, ): Promise<string> { const current = await repository.getActive(tenantId, userId); const nextKey = avatarKey(tenantId, userId, uploadId); await store.put(nextKey, bytes, contentType); await repository.activate( tenantId, userId, nextKey, current?.objectKey ?? null, retainUntil, ); return nextKey; } 

The missing pieces are policy checks, not decorative plumbing: content type, maximum size, authorization, idempotency, and a worker that records each state transition. Keep raw image data out of logs. Log the tenant ID, user ID, object key, upload ID, and the transition so an incident review can answer which tenant owned the candidate and which pointer was active.

When is overwriting the old key the better trade-off?

Overwrite only when the product can honestly offer no rollback, the image is disposable, and concurrent writers are controlled elsewhere. A cache whose source of truth is another system can fit that rule. A tenant-facing avatar usually does not: a misordered write turns an application mistake into lost content.

The catch is that application-level retained objects are not storage-level immutability. If a regulator requires object lock, legal hold, or a write-once guarantee, this design is not suitable by itself. Choose a storage system with the documented control and test the policy with the compliance owner. Stick with a native storage workflow when provider-specific versioning, object lock, cross-region replication, browser-upload policy controls, or public-read delivery is part of the contract.

There is an operational cost to keeping predecessor keys. Track activation latency, candidate age, orphan count, cleanup lag, delete failures, and cross-tenant authorization denials. Storage pricing and multipart behavior should be checked against the current service documentation, not guessed from a small test. The exact retention period is a product and compliance decision; it is not a filename convention.

The practical rule is short: immutable keys protect bytes, a tenant-scoped pointer chooses the visible image, and delayed deletion preserves the rollback window. Delete an old key only after the application has stopped promising to recover it.

Sources