Laravel Nova - How to remove image from disk when corresponding database entry is removed?

I'm new to Laravel and Nova and want to built a Resource that allows the storing of an image, together with some other data. After upload, the image file is stored on the disk (storage/app/public) while all other data (including the image link) is stored in the database.

stored image:

enter image description here

database:

enter image description here

When I remove this database entry in Nova the stored image is unaffected by it. Won't this overflow my storage in production? Does Nova come with a way to clear unused images from disk or do I have to manually remove it with Laravel? In either case I'd appreciate help.

This is the fields function of my Resource:

public function fields(Request $request)
{
    return [
        ID::make(__('ID'), 'id')->sortable(),
        Text::make('Heading'),
        Text::make('Content'),
        File::make('Image')->disk('public'),
    ];
}

Upvotes: 2

Views: 968

Answers (1)

The Nova doc provides a simple solution for this by adding prunable to the Image function (or any other File function):

Image::make('Profile Photo')->disk('public')->prunable()

This way images are deleted from the storage when image is deleted or updated.

Upvotes: 3

Related Questions