Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using model->delete() does not seem to invoke a call to delete all attached media. #3767

Open
repomic opened this issue Jan 13, 2025 · 0 comments

Comments

@repomic
Copy link

repomic commented Jan 13, 2025

Calling model->delete() does not clear images in storage. Have to call clearMediaCollection() before calling delete() on model.

class Item extends Model implements HasMedia
{
    use HasFactory, InteractsWithMedia, HasSlug;

    public function registerMediaConversions(?Media $media = null): void
    {
        $this->addMediaConversion('thumbnail')
            ->width(368)
            ->height(232)
            ->sharpen(10)
            ->nonQueued();
    }
}

$item = Item::factory()->create([]);
$image = fake()->image(dir: storage_path('app/public/items'), width: 1024, height: 1024, category: 'flowers');
$item->addMedia($image)->preservingOriginal()->toMediaCollection();

Not working:

$item->delete();
public> php artisan media-library:clean  
Output: Orphaned media directory `1` has been removed

Working:

$item->clearMediaCollection();
$item->delete();
public> php artisan media-library:clean
Output: All done!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant