@@ -264,9 +264,24 @@ class User(BaseUser):
264264 # elapsed.
265265 delete_permanently_at = models .DateTimeField (null = True , editable = False )
266266
267+ def delete_cached_thumbnail (self ):
268+ """Delete the cached ImageKit thumbnail so it regenerates on next access."""
269+ if not self .profile_image :
270+ return
271+ try :
272+ from imagekit .cachefiles .backends import CacheFileState
273+
274+ thumb = self .image_thumbnail
275+ if thumb .name :
276+ thumb .storage .delete (thumb .name )
277+ thumb .cachefile_backend .set_state (thumb , CacheFileState .DOES_NOT_EXIST )
278+ except (OSError , AttributeError ):
279+ logger .debug ("Failed to invalidate thumbnail cache" , exc_info = True )
280+
267281 def save_image_from_provider (self , avatar_url ):
268282 from django .core .files .base import ContentFile
269283
284+ self .delete_cached_thumbnail ()
270285 response = requests .get (avatar_url )
271286 filename = f"{ self .profile_image_filename_root } .png"
272287 self .profile_image .save (filename , ContentFile (response .content ), save = True )
@@ -286,7 +301,7 @@ def claim(self):
286301 def get_thumbnail_url (self ):
287302 # convenience method for templates
288303 if self .profile_image and self .image_thumbnail :
289- with suppress (AttributeError , MissingSource ):
304+ with suppress (AttributeError , MissingSource , FileNotFoundError , OSError ):
290305 return getattr (self .image_thumbnail , "url" , None )
291306
292307 def get_avatar_url (self ):
@@ -308,7 +323,7 @@ def get_avatar_url(self):
308323 def get_hq_image_url (self ):
309324 # convenience method for templates
310325 if self .hq_image and self .hq_image_render :
311- with suppress (AttributeError , MissingSource ):
326+ with suppress (AttributeError , MissingSource , FileNotFoundError , OSError ):
312327 return getattr (self .hq_image_render , "url" , None )
313328
314329 @property
@@ -340,10 +355,10 @@ def delete_account(self):
340355 self .last_name = "Doe"
341356 self .display_name = "John Doe"
342357 self .email = "deleted-{}@example.com" .format (uuid .uuid4 ())
358+ self .delete_cached_thumbnail ()
343359 image = self .profile_image
344360 transaction .on_commit (lambda : image .delete ())
345361 self .profile_image = None
346- self .image_thumbnail = None
347362 self .delete_permanently_at = None
348363 self .save ()
349364
0 commit comments