Delete file from a specific directory in Laravel local storage

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Deleting Files from Laravel Local Storage: A Developer's Guide

When building modern web applications with Laravel, managing file uploads and deletions is a common requirement. Developers often store assets in the /storage/app/public directory and then use php artisan storage:link to expose them publicly via the /public/storage URL. While this setup makes files accessible for viewing in the browser, figuring out how to programmatically delete those files can sometimes be confusing.

This post will walk you through the correct, robust way to delete files stored within your Laravel local storage structure, specifically addressing scenarios involving public links.

Understanding Laravel Storage and Disks

Before diving into deletion, it’s crucial to understand how Laravel's Storage facade works. The Storage class is an abstraction layer that allows you to interact with various file systems (disks) defined in your configuration. By default, Laravel uses the local disk for storing files within storage/app.

When you use php artisan storage:link, you are creating a symbolic link that maps the contents of storage/app/public to the publicly accessible /public/storage directory. This mechanism is powerful but requires understanding the underlying path structure.

The key insight here is that the methods provided by the facade, like delete(), operate directly on the file system paths defined by your disk configuration.

The Correct Way to Delete Files

Your attempt to use $files = Storage::files($path); was useful for listing files, but it doesn't execute a deletion command. To perform a deletion, you need to call the dedicated delete() method on the appropriate file path within your storage structure.

The crucial step is constructing the path correctly. Since your files reside in the application's internal storage directory (storage/app/public/...), you must reference this path using the Storage facade methods.

Step-by-Step Deletion Example

Let’s assume your file structure is: /storage/app/public/userId/images/test.jpg.

To delete test.jpg, you need to tell Laravel exactly where that file lives on the disk. You use the format disk_name->delete('path'). Since the public links are usually tied to the default local disk, you can often simplify this, but being explicit is always safer.

Here is how you would correctly delete a specific file:

use Illuminate\Support\Facades\Storage;

class FileController extends Controller
{
    public function deleteImage(int $userId, string $fileName)
    {
        // 1. Construct the relative path within the 'public' storage directory
        // We reference the folder structure where the file is located.
        $path = "userId/images/{$fileName}";

        // 2. Use the delete method on the Storage facade
        if (Storage::disk('public')->exists($path)) {
            if (Storage::disk('public')->delete($path)) {
                return response()->json(['message' => 'File deleted successfully!']);
            } else {
                return response()->json(['message' => 'Error deleting file.'], 500);
            }
        }

        return response()->json(['message' => 'File not found.'], 404);
    }
}

Explanation of the Code

  1. Storage::disk('public'): This explicitly tells Laravel to operate on the disk configured for your public assets (which is often set up via the filesystems.php configuration). This ensures you are interacting with the files linked by your storage link.
  2. exists($path): It is a best practice to check if the file exists before attempting deletion. This prevents unnecessary errors and allows you to provide meaningful feedback to the user (e.g., HTTP 404).
  3. delete($path): This command executes the removal of the file from the filesystem. By using the relative path structure (userId/images/test.jpg), Laravel correctly navigates the storage hierarchy and removes the file entirely.

Best Practices for File Management

When dealing with file operations in Laravel, remember these best practices:

  • Use Relative Paths: Always construct your paths relative to the root of the disk you are operating on (e.g., userId/images/test.jpg), not absolute system paths. This makes your code portable and resilient across different environments.
  • Permissions: Ensure the web server user (e.g., www-data or apache) has the necessary write and delete permissions for the /storage directory on your server. File permission issues are a common pitfall when dealing with storage operations.
  • Eloquent Integration: If you are using Eloquent models to manage file relationships, consider storing the file path or filename directly in the database rather than relying solely on filesystem paths. This provides an additional layer of data integrity and makes backups simpler.

Conclusion

Deleting files from Laravel's local storage is straightforward once you understand the facade methods. By correctly utilizing Storage::disk('public')->delete($path) and implementing checks for file existence, you can manage your public assets efficiently and securely. Mastering these filesystem interactions is a core skill in leveraging the full power of Laravel, as highlighted by resources like those found on laravelcompany.com.