Laravel delete directory from public

Laravel delete directory from public

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Storage Facade and the Public Directory Trap

When attempting to remove files or directories within a Laravel application, it is easy to assume that the Storage facade operates on a single, monolithic filesystem. However, relying on Storage::deleteDirectory($directory) in the context of your public folder often leads to unexpected results because this method targets the directory specified relative to the root of the storage disk by default.

The core issue lies in how Laravel's filesystem abstraction handles paths. The Storage::deleteDirectory() method expects a path relative to the configured disk. If you are using the default local disk, which typically maps to the storage/app directory, passing a path like public/images will cause Laravel to look for a folder at storage/app/public/images. Since that location rarely exists or contains what you intend to delete, the operation fails silently or throws an exception.

To successfully remove content from the public folder using the filesystem abstraction layer, you must explicitly define which disk you are interacting with. The solution involves retrieving the specific disk instance for the public directory and then invoking the deletion method on that instance. Here is how you should structure the call:

use Illuminate\Support\Facades\Storage;

// Delete a directory in the public folder
Storage::disk('public')->deleteDirectory($directory);

By chaining the deleteDirectory() method to Storage::disk('public'), you are instructing Laravel to resolve the path relative to /storage/app/public (or wherever your SYSLINK or symlink configuration points) instead of /storage/app. This distinction is critical when handling user-uploaded assets, cached views, or compiled assets that reside in the public directory.

Manual Filesystem Operations

While the filesystem abstraction layer is powerful for managing data within the application's storage hierarchy, it cannot be used to manipulate the public directory if you need to delete entries outside of Laravel's managed disks. The public folder is often a symlinked view of the actual source code or a separate directory intended for static assets.

If your architecture involves deleting files directly from the web root without using the Storage facade, you must rely on PHP's native filesystem functions. This approach bypasses the Laravel abstraction entirely but offers direct control over the operating system's file structure. You can use rmdir() to remove directories and unlink() for files:

$directoryPath = public_path($directory);

// Check if directory exists before attempting deletion
if (is_dir($directoryPath)) {
    // rmdir only removes empty directories by default in older PHP versions
    // In modern PHP, you might need to ensure it is empty first or use a helper
    if (emptyDir($directoryPath)) {
        rmdir($directoryPath);
    } else {
        throw new RuntimeException("Directory $directory is not empty and cannot be deleted.");
    }
}

It is vital to remember that rmdir() will fail if the directory contains any files or subdirectories. Before calling this function, you often need to recursively delete the contents first. Failing to handle non-empty directories results in a DirectoryNotEmpty exception, which can crash a request handler if not caught gracefully.

Security Considerations and Permissions

A significant pitfall when deleting from the public folder is permission handling. The web server process (often running as www-data, _www, or nginx) requires write permissions to delete files in the public directory. If your application runs under a specific user context but the public folder is owned by root, deletion attempts will fail with a Permission denied error.

Always ensure that the directory ownership and permissions align with your deployment strategy. You should verify that the web server process has read/write/execute access to the parent directories and write access to the target directory specifically. Additionally, if you are deleting sensitive data or user uploads, consider whether this operation needs to be atomic or if a transaction-like approach is required to maintain data integrity during cleanup routines.

If you find yourself managing complex storage logic or needing robust solutions for asset management in Laravel, resources like LaravelCompany offer guidance on best practices for handling file systems and security configurations.

Our network