Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Deleting file after downloading via Storage::download() in Laravel 10

Hello,

How can I delete a file after a download response in the controller?

public function some_method(Request $request)
{
    // ...
    
    return Storage::download('file.jpg'); // <-- delete after download
}

I found older posts for very old Laravel versions that I could use deleteFileAfterSend, but I am not sure I can use it with Laravel 10 anymore.

what can I do instead?

Thanks

0 likes
2 replies
hupp's avatar
hupp
Best Answer
Level 11

@ligonsker

Try this

public function downloadFile($filename)
{
    // Generate a temporary URL for the file download
    $temporaryUrl = Storage::temporaryUrl(
        "path/to/{$filename}",
        now()->addMinutes(5) // Adjust the expiration time as per your requirement
    );

    // Return the download response
    return response()->download($temporaryUrl, $filename)->deleteFileAfterSend();
}

Let me know your feedback.

2 likes
Ligonsker's avatar

@hupp yes that worked instead of using the Storage facade. thank you.

1 like

Please or to participate in this conversation.