DEV Community

Cover image for How To Delete Files after Download in Laravel 8
Code And Deploy
Code And Deploy

Posted on • Updated on

How To Delete Files after Download in Laravel 8

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-delete-files-after-download-in-laravel-8

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

In my previous post, I share how to download files in Laravel. Now I will share how to delete your file after downloading. Luckily Laravel 8 already provided a function to delete the file directly after sending it to the user end.

With the help of this function deleteFileAfterSend(true) a chaining method for Response class in Laravel 8 our task is done!

Below is the complete code on how to use it.

public function download() {
    $path = public_path('for_pro_members.zip');
    $fileName = 'purchase_files.zip';

    return Response::download($path, $fileName, ['Content-Type: application/zip'])->deleteFileAfterSend(true);
}
Enter fullscreen mode Exit fullscreen mode

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-delete-files-after-download-in-laravel-8 if you want to download this code.

Happy coding :)

Top comments (0)