Laravel-medialibrary: I want to delete the media by its id not another model id.

Created on 12 Sep 2017  路  12Comments  路  Source: spatie/laravel-medialibrary

I want to delete the media by its id not another model id. I have media id 1 in media table. So my problem in i want to delete that media by its own id.

$media = \App\Media::find(1)->delete();

it delete database record but not folder and files.

Most helpful comment

I was just having the same problem. My uploads are local and I was trying to delete the media by its id directly

the following would delete the database row in the media table, but leave all of the uploads themselves, conversions and all, untouched

$media = Media::find($request->input('id'));
$media->delete();

after searching for the answer, I found that the only way to get the files deleted is to issue the delete command via the associated model. Since all I had was a media ID however I had to do a bit of reverse work

$media = Media::find($request->input('id'));
$model = Model::find($media->model_id);
$model->deleteMedia($media->id);

This deletes the database record as well as all the files/conversions and media-folder

All 12 comments

The tests seem to prove that deleting a media object will deleted the folder and files

https://github.com/spatie/laravel-medialibrary/blob/0491a18d95be3b8ef3e2e351cd14720d7484070a/tests/Media/DeleteTest.php#L13-L22

Could you PR a failing test?

Still record delete, file doesn't delete.

Hi @shankhadevpadam, is your media being stored on the local filesystem?

yes inside public/uploads

What OS are you using? Might be a filesystem permission issue, can you check if you can manually delete the files using the Storage facade?

Storage::deleteDirectory(public_path('uploads/1'));

I was just having the same problem. My uploads are local and I was trying to delete the media by its id directly

the following would delete the database row in the media table, but leave all of the uploads themselves, conversions and all, untouched

$media = Media::find($request->input('id'));
$media->delete();

after searching for the answer, I found that the only way to get the files deleted is to issue the delete command via the associated model. Since all I had was a media ID however I had to do a bit of reverse work

$media = Media::find($request->input('id'));
$model = Model::find($media->model_id);
$model->deleteMedia($media->id);

This deletes the database record as well as all the files/conversions and media-folder

That's strange... $model->deleteMedia() simply finds and deletes() the media as well.

I just added a passing test for deleting the Media instance directly, just to be sure:
https://github.com/spatie/laravel-medialibrary/blob/e281b9dc7722258493e91005817813539afd6bba/tests/Media/DeleteTest.php#L25-L35

Closing to due to inactivity of OP.

My solution:

Model::whereHas('media', function ($query) use($media_id){
     $query->whereId($media_id);
})->first()->deleteMedia($media_id);

A little improvement to @vesper8 solution if you want to dynamically query the model by its model_type string

$media = Media::find($request->input('id'));

$model_type = $media->model_type;

$model = $model_type::find($media->model_id);
$model->deleteMedia($media->id);

@cyberfly for example, I have 4 images and each image has a remove/delete button nd when u click on the delete button it should remove just that image and its related conversion both from db and from the directory. Your solution alongside others would remove the image from db however, it would also delete all images in the folder.
so my question is - is there a way to remove each image using medialibrary?
e.g 5 images, user remove 1, remaining images should be 4 inside the folder, but currently, the folder that contains the images to that model wud be deleted. Any hint for this?

 try this
// delete previous image in the database
                $query = DB::table('media')->where('id', $media_id);
                if ($query->count() > 0) :
                    $query->delete();
                endif;

                $path= storage_path("app/public/" . $media_id);
                //remove folder
                $this->rmdir_recursive($path);

protected function rmdir_recursive($dir)
    {
        foreach (scandir($dir) as $file) {
            if ('.' === $file || '..' === $file) continue;
            if (is_dir("$dir/$file")) $this->rmdir_recursive("$dir/$file");
            else unlink("$dir/$file");
        }
        rmdir($dir);
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

netanelwebninja picture netanelwebninja  路  3Comments

mohammad6006 picture mohammad6006  路  4Comments

aaronfullerton picture aaronfullerton  路  4Comments

snapey picture snapey  路  3Comments

brendt picture brendt  路  4Comments