Laravel File Storage delete all files in directory

Laravel File Storage delete all files in directory

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

Stefan Bogdanescu

Founder & Senior Architect

Laravel File Storage delete all files in directory

When you need to purge a specific directory like storage/app/backgrounds, the immediate instinct might be to look for a dedicated method that accepts a path and wipes it clean. Unfortunately, Laravel's Storage facade does not provide a built-in .deleteAll() or .wipe() function. The official documentation for deleting files focuses on individual paths or glob patterns, which leads developers to explore alternative strategies depending on their specific constraints.

The most straightforward approach within the framework relies on glob pattern matching. If you are working with legacy code or a specific configuration where you need to delete only files matching a certain extension, the delete() method accepts a wildcard string. For instance, to remove all JPG images from your backgrounds folder, you can pass a glob pattern directly to the static Storage::delete() method:

use Illuminate\Support\Facades\Storage;

Storage::disk('local')->delete('backgrounds/*.jpg');

This approach is efficient for targeted cleanup but requires you to know the exact file extensions beforehand. If your directory contains mixed media types—such as PNGs, GIFs, or raw data files—using a wildcard like * will delete everything recursively, including subdirectories if they exist within the path. This behavior can be dangerous in production environments where you might inadvertently remove configuration backups or log files stored alongside your assets.

For more robust scenarios where you need to ensure every file in a directory is removed regardless of extension, using the filesystem's underlying capabilities via helper methods provides better control. The files facade, which wraps the Symfony Filesystem component used by Laravel, offers a .deleteDirectory() method that behaves exactly as needed: it removes all contents within a specified directory and then deletes the directory itself if it is empty.

Here is how you can apply this to clear your backgrounds folder safely:

use Illuminate\Support\Facades\Storage;
use Symfony\Component\Finder\Finder;

// Option 1: Using the files facade for recursive deletion
$disk = Storage::disk('local');
$files = $disk->allFiles('backgrounds');

foreach ($files as $file) {
    $disk->delete($file);
}

// Optionally remove the directory itself if it's empty afterward
if ($disk->exists('backgrounds') && $disk->isDir('backgrounds')) {
    $disk->deleteDirectory('backgrounds');
}

This pattern iterates through every file found in the target path and deletes them individually. While slightly more verbose than a single magic function, it guarantees that you are only removing files and gives you the opportunity to handle exceptions or log deletions per item if required.

If you prefer a lower-level solution without iterating in PHP, you can leverage the Symfony Finder component directly to locate files and delete them in a loop, which remains compatible across Laravel versions. However, be mindful that large directories with thousands of small files might trigger memory spikes during iteration. In such cases, consider processing deletions in batches or using shell commands executed via exec() for massive cleanup tasks, though this bypasses Laravel's exception handling entirely.

A critical pitfall to avoid is assuming that deleting a directory automatically removes its contents in all contexts. The delete() method on a directory path often fails silently or throws an exception depending on the driver (local vs. S3). Always verify the disk type and ensure you have write permissions before executing bulk deletion logic. Furthermore, if you are using Laravel's database cache to store file paths, remember to clear your application cache after wiping physical files to prevent stale references in your data models.

For teams managing production workflows, this functionality is often wrapped in a dedicated maintenance command or a controller action with proper authorization checks. Before finalizing any bulk deletion operation, it is prudent to backup the directory contents or at least verify the file count to ensure you are not removing critical assets unintentionally.

Our network