Table of contents
![Deleting files after download in laravel](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/how-to-delete-files-after-download-in-laravel-8x980.webp)
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);
}
Â
I hope it helps. Thank you for reading.
Â
Cheers :)
Read next