Skip to content

[12.x] Add storage:clear Artisan command to delete files or folders on a configured disk #55719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 12.x
Choose a base branch
from

Conversation

sawan-webmavens
Copy link

@sawan-webmavens sawan-webmavens commented May 12, 2025

Added storage:clear Artisan command

This PR introduces a new Artisan command: storage:clear, which allows developers to:

🚀 Purpose

Effortlessly clear files and folders from a specified filesystem disk, with fine-grained control and safety mechanisms. Ideal for cleaning up temporary files, logs, exports, and more—across both local and cloud disks.

🛠️ Features

✅ Clear all files from a given disk.
✅ Target a specific folder within the disk for selective cleanup.
✅ Preserve .gitignore files to retain version control best practices.
✅ Use --force to skip confirmation prompts in production environments.
✅ Detailed feedback when:

  • Disk is not configured
  • Folder is not found
  • Operation is skipped due to no confirmation

📦 Usage

php artisan storage:clear
php artisan storage:clear --disk=local
php artisan storage:clear --disk=s3 --folder=foldername
php artisan storage:clear --disk=s3 --folder=somefolder/foldername
php artisan storage:clear --disk=local --folder=foldername --force //Force the operation to run when in production

💡 Use Cases

Use Case Example Command Benefit
🧹 Clean up temp uploads php artisan storage:clear --disk=local --folder=tmp --force Automates deletion of temporary files in queues/schedules
☁️ S3 log rotation php artisan storage:clear --disk=s3 --folder=logs --force Clean up exports/logs on cloud storage with no extra code
🛠️ Local dev reset php artisan storage:clear --disk=local Quickly reset dev environment
🚀 CI/CD deployment hygiene php artisan storage:clear --disk=local --folder=build --force Ensures clean slate before deploys
🔐 Protection in production Run without --force Prevents accidental data loss via confirmation prompts
🧯 Misconfigured disk detection Invalid --disk option Fails early with descriptive error if disk not defined

🧪 Sample Output

> php artisan storage:clear --disk=local --folder=staging

Cleared folder [staging] on disk [local]
> php artisan storage:clear --disk=unknown

❌ Disk [unknown] is not configured. Available disks: local,s3.
> php artisan storage:clear --disk=s3 --folder=archive

⚠️ You're running in production. Use --force to confirm.
Operation cancelled.

@sawan-webmavens sawan-webmavens marked this pull request as draft May 12, 2025 14:59
@sawan-webmavens sawan-webmavens marked this pull request as ready for review May 13, 2025 11:36
@gabrielrbarbosa
Copy link
Contributor

I use something like this in my schedule today:

Schedule::call(function () {
    Storage::disk('local')->delete(Storage::disk('local')->allFiles());
})->dailyAt('02:00');

maybe this command could use storage facade instead of clearFolder and clearDisk methods?

@sawan-webmavens
Copy link
Author

@gabrielrbarbosa Thats one way to do it, if you know you want to clear them daily. A lot of times I end up clearing storage on-demand or only certain folders within the storage. The command expands usability of storage facade.

I will add more uses cases that I am thinking of in original PR description

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants