Reference

Mixins

class django_admin_helpers.mixins.FilesDeleteMixin

Mixin for ModelAdmin classes to delete files along with model instances.

By default, Django leaves the files of FileField (and ImageField) in storage when the model instances referencing them are deleted. This mixin hooks into the admin deletion flow to also delete the files from storage, both when deleting a single instance from its change page and when using the bulk delete action from the change list.

Set the file_field_names attribute to the list of file field names to delete (defaults to ["file"]):

class DocumentAdmin(FilesDeleteMixin, admin.ModelAdmin):
    file_field_names = ["file", "thumbnail"]

If a file cannot be deleted, the error is logged and the instance is deleted from the database anyway.

delete_model(request: HttpRequest, obj: Model) → None

Delete the file(s) associated with the instance.

delete_queryset(request: HttpRequest, queryset: QuerySet) → None

Delete the file(s) associated with the instances in the queryset.