How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Delete Files from the Public Folder in Laravel
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Delete Files from the Public Folder in Laravel
Web Development

How to Delete Files from the Public Folder in Laravel

how7o
By how7o
Last updated: May 10, 2026
7 Min Read
Laravel delete file from public folder — File::delete with public_path
SHARE

To laravel delete file public folder — typically a thumbnail or upload tied to a model you’re removing — use Laravel’s File facade with public_path(). The model’s delete() clears the database row but leaves the on-disk file behind unless you explicitly unlink it. This guide covers the single-file case, multi-file deletes, the Storage-vs-File decision, and how to clean up whole directories safely.

Contents
  • TL;DR
  • The full destroy method
  • Deleting multiple files at once
  • File::delete vs Storage::delete
  • Cleaning up whole directories
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on Laravel 11 with PHP 8.3. Originally published 2022-08-17, rewritten and updated 2026-04-23.

TL;DR

Inside a controller’s destroy method, after you’ve loaded the model but before (or after) calling $post->delete():

use Illuminate\Support\Facades\File;

$thumbnail = public_path("uploads/{$post->thumbnail}");
if (File::exists($thumbnail)) {
    File::delete($thumbnail);
}

The full destroy method

A typical blog-post deletion where the thumbnail lives at public/uploads/:

use Illuminate\Support\Facades\File;

public function destroy($id)
{
    $post = Post::findOrFail($id);

    $thumbnail = public_path("uploads/{$post->thumbnail}");
    if (File::exists($thumbnail)) {
        File::delete($thumbnail);
    }

    $post->delete();

    return redirect('admin/posts')
        ->with('message', 'Post deleted successfully!');
}

Three things to notice:

  • public_path('uploads/...') builds an absolute filesystem path — /var/www/blog/public/uploads/abc123.jpg — which is what File::delete() needs. Building the path yourself with string concatenation ('/public/uploads/' . $name) breaks the moment the app runs in a different environment.
  • The File::exists() guard is optional. File::delete() returns false for a missing file and doesn’t throw — so skip the check unless you want to log the orphan case.
  • Delete the file before the model row is removed. If the delete() call somehow fails, you’d rather have a zombie row in the database (recoverable) than a zombie file with no row pointing at it (harder to garbage-collect).
laravel delete file public folder — File::delete with public_path versus Storage::delete on a disk

Deleting multiple files at once

File::delete() accepts a variadic list or an array:

File::delete($thumbnail1, $thumbnail2, $thumbnail3);

// or, when you've built a list programmatically
$thumbnails = [$thumbnail1, $thumbnail2, $thumbnail3];
File::delete($thumbnails);

Useful when you have multiple image sizes saved alongside a model (a full-size, a card thumbnail, an OG preview). The return value is true only if every file deleted successfully — false if any one was missing or couldn’t be removed.

File::delete vs Storage::delete

Laravel has two file APIs that do very similar things. Pick based on where the file lives:

  • File::delete() — works with absolute filesystem paths (what public_path(), base_path(), storage_path() return). Use this when you’re managing files directly under public/.
  • Storage::delete() — works with disk-relative paths on a configured filesystem (config/filesystems.php). Use this when files were saved through Storage::disk('public')->put() or a model store() call — it dispatches to the local filesystem, S3, or any other driver based on the disk.

For a file saved via $request->file('thumbnail')->store('thumbnails', 'public'), the matching delete is:

use Illuminate\Support\Facades\Storage;

Storage::disk('public')->delete($post->thumbnail);
// $post->thumbnail is what store() returned, e.g. "thumbnails/abc123.jpg"

The public disk’s root is storage/app/public/, symlinked into public/storage/ via php artisan storage:link. This is Laravel’s recommended layout for user uploads — keeps them out of version control and makes S3 migration trivial.

Cleaning up whole directories

For periodic cleanup (temp exports, expired caches), reach for directory-level helpers:

// Remove the directory and everything under it
File::deleteDirectory(public_path('uploads/temp'));

// Keep the directory but empty it
File::cleanDirectory(public_path('uploads/temp'));

Both are destructive. Treat them the same way you’d treat rm -rf: never pass a path that contains user-supplied segments without first validating that the resolved path stays inside the intended base directory. A typical guard:

$base   = realpath(public_path('uploads'));
$target = realpath(public_path("uploads/{$userInput}"));

if ($target === false || !str_starts_with($target, $base . DIRECTORY_SEPARATOR)) {
    abort(403);  // path traversal attempt
}

File::deleteDirectory($target);

The realpath resolution expands any ../ segments; the str_starts_with check makes sure the result is still inside the intended base.

Frequently asked questions

What’s the simplest way to laravel delete file public folder?

File::delete(public_path('uploads/filename.jpg')), with use Illuminate\Support\Facades\File; at the top of the file. It wraps PHP’s unlink and returns true on success, false if the file didn’t exist. For multiple files, pass them as separate arguments or as an array — File::delete($a, $b, $c) or File::delete([$a, $b, $c]).

Should I check File::exists() first?

Only if you need to know whether the file was there. File::delete() quietly returns false for a missing file and doesn’t throw, so for a best-effort cleanup (“delete the thumbnail if it’s there”) you can call it directly. Add the exists() check when you want to log or alert on the unexpected case — for example, a model that claims to have a thumbnail but the file is gone.

File::delete vs unlink vs Storage::delete — which one?

Use File::delete for files you built a path to with public_path() or base_path() — it’s a thin wrapper around unlink that accepts arrays. Use Storage::delete when you’ve stored the file on a configured filesystem disk (storage/app/public, S3, etc.) and you only have a relative path on that disk. unlink is the plain PHP call and works fine too, but File::delete is more idiomatic and handles arrays for free.

How do I delete a file saved via Laravel’s Storage facade?

Use Storage::disk('public')->delete($relativePath) where $relativePath is what store() or storeAs() returned — typically something like thumbnails/abc123.jpg. Don’t pass the full public_path() value; Storage deals in disk-relative paths. If your model saved the store() return value, that’s already the right shape.

Can I delete all files in a public subfolder at once?

File::deleteDirectory(public_path('uploads/old')) removes a directory and everything under it. File::cleanDirectory(public_path('uploads/old')) empties the directory but leaves it in place. Both are destructive — guard them behind the same checks you’d give rm -rf: validate the path doesn’t escape your uploads root before calling, especially if any part of the path is user-supplied.

Related guides

  • How to Delete a Record from the Database in Laravel with Eloquent — the model-side of a cleanup flow.
  • How to Automatically Delete All Related Rows in Laravel Eloquent — cascading deletes so related rows (and their files) go together.
  • Fix 403 Forbidden on Laravel Shared Hosting — fix the upload path before worrying about the delete path.
  • How to Install Laravel on Ubuntu — set up storage:link and the public disk on a fresh install.

References

Official Laravel filesystem docs (File, Storage, disks): laravel.com/docs/filesystem.

TAGGED:Laravelphp

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Laravel cURL error 60 SSL certificate problem — CA bundle wiring in php.ini How to Fix cURL Error 60 SSL Certificate Problem in Laravel
Next Article Laravel request inputs with prefix — filter request()->all() by Str::startsWith How to Retrieve Inputs with a Specific Prefix in Laravel Request
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Laravel old() helper for repopulating form inputs
Web Development

How Laravel’s old() Helper Works (and Why It Sometimes Doesn’t)

6 Min Read
WordPress check if user is logged in with is_user_logged_in()
Web Development

How to Check If a User Is Logged In in WordPress

7 Min Read
Laravel Eloquent multiple where and orWhere — closure-grouped query snippet with parenthesis highlight
Web Development

How to Combine Multiple where() and orWhere() in Laravel Eloquent

7 Min Read
Laravel unique validation that ignores the current record on update
Web Development

How to Validate a Unique Column on Update in Laravel

4 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?