Skip to content

[2.x] fix: delete avatar file from disk when user account is deleted#4485

Merged
imorland merged 1 commit into
2.xfrom
im/delete-avatar-on-user-delete
Mar 22, 2026
Merged

[2.x] fix: delete avatar file from disk when user account is deleted#4485
imorland merged 1 commit into
2.xfrom
im/delete-avatar-on-user-delete

Conversation

@imorland

Copy link
Copy Markdown
Member

Summary

Closes #4459.

When a user account was deleted, their avatar file was never cleaned up from the flarum-avatars disk. AvatarUploader::remove() was only called via the explicit avatar delete flow — not during user account deletion. Over time this accumulates orphaned files with no recovery path.

Fix

Hook into the existing deleting observer in User::boot() (pre-delete, so avatar_url is still readable) and delete the file directly from the filesystem using the same resolve(Factory::class)->disk('flarum-avatars') pattern already used in the model.

Using deleting rather than deleted is intentional — by the time deleted fires the model attributes may no longer reflect the original DB state. getRawOriginal('avatar_url') reads the stored path rather than going through the accessor (which would return a full URL).

Test plan

  • Upload an avatar for a user, note the filename in flarum-avatars storage
  • Delete the user account
  • Confirm the avatar file is no longer present on disk
  • Confirm deleting a user with no avatar does not error
  • Confirm root admin (id=1) still cannot be deleted

AvatarUploader::remove() was never called during user deletion, leaving
avatar files as orphans on the flarum-avatars disk. Every deleted user
with a custom avatar accumulated a stranded file indefinitely.

Hook into the existing deleting observer (pre-delete, so the avatar_url
attribute is still readable) and delete the file directly from the
filesystem. Uses getRawOriginal() to get the stored path rather than
the accessor, which would return a full URL.

Fixes #4459
@imorland imorland changed the title fix(user): delete avatar file from disk when user account is deleted [2.x] fix: delete avatar file from disk when user account is deleted Mar 22, 2026
@imorland imorland added this to the 2.0.0-beta.8 milestone Mar 22, 2026
@imorland imorland marked this pull request as ready for review March 22, 2026 10:28
@imorland imorland requested a review from a team as a code owner March 22, 2026 10:28
@imorland imorland merged commit 1777032 into 2.x Mar 22, 2026
25 checks passed
@imorland imorland deleted the im/delete-avatar-on-user-delete branch March 22, 2026 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[2.x] Avatar files are not deleted from disk when a user account is deleted

1 participant