[Django]-Django storages aws s3 delete file from model record

36👍

You have to explicitly delete the file. You could write a post delete signal or do it in the delete_employee function.

employee.upload.delete(save=False)  # delete file
employee.delete()  # delete model instance

The docs for FileField.delete() explains this.

Note that when a model is deleted, related files are not deleted. If you need to cleanup orphaned files, you’ll need to handle it yourself (for instance, with a custom management command that can be run manually or scheduled to run periodically via e.g. cron).

You should also make sure that there’s no other FileField that references the exact same file before deleting it.

2👍

For all of my broski who struggle to delete the folder with django-storages.

Let’s consider a real case. I have a dynamic path that each file stored in the folder and I had to implement a cleanup.

def get_upload_path(instance, filename):
    return os.path.join(
        'organizations',
        'files',
        str(instance.organization.pk),
        str(instance.hash),
        str(filename)
    )

file = models.FileField(
    upload_to=get_upload_path
)

The problem with my case was that I could not delete a folder with django-storages during the cleanup. instance.file.name raises an error because you cannot have an absolute path to the file with django-storages.
In order to delete a folder, you should use storage.delete() because since you cannot have an absolute path you cannot delete a folder with a straightforward manner (for example shutil.rmtree(...)).

My clean-up implementation is a little complex but it’s solid. In my case, I used the pre_delete signal and advised you to do the same.


from django.core.files.storage import get_storage_class

default_storage = get_storage_class()()

@receiver(pre_delete, sender=OrganizationFile)
def delete_has_folder(sender, instance, *args, **kwargs):
    # get filename that will be equals to the relative path but not actually the filename
    path = Path(instance.file.name)
    # get a parent folder str
    folder_path = str(path.parent)
    
    # delete a file
    instance.file.delete()
    # delete a folder. 
    #  shutil.rmtree(absolute_path) wouldn't work
    #  because instance.file.path will raise an error.
    #  hence the only way is to delete with a storage default_storage.delete
    default_storage.delete(folder_path)

    logger.info(f'Pre delete {instance}. Deleted the hash folder {folder_path}')

0👍

I used this:

import boto3

client = boto3.client('s3')
client.delete_object(Bucket='mybucketname', Key='myfile.whatever')

But I’m trying to find a way to do it with the ImageFile object of my model, or maybe with some configuration of my storage class:

from storages.backends.s3boto3 import S3Boto3Storage 

class MediaStorage(S3Boto3Storage):    
    location = 'media'    
    file_overwrite = True

Leave a comment