Dylan Heitz Dylan Heitz - 1 year ago 89
PHP Question

Delete files/uploads when delete User

I have my Users table (with name, email, password, etc...) and the users can upload pictures, files or videos.

Their pictures are stored in specific path (for example


I wondered if the files uploaded by the users can be deleted when the row of the user is deleted.

The big issue is that I want to delete the data that the users have uploaded when I do
php artisan migrate:refresh --seed

I tried this on the migration file

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

use File;

class CreateUsersTable extends Migration
* Run the migrations.
* @return void
public function up()

Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';

* Reverse the migrations.
* @return void
public function down()
// Deleting ALL the data that the users have uploaded
$dir = public_path(). "/uploads/";
File::deleteDirectory($dir, true);

But I have the following error when I try to execute
php artisan migrate:refresh --seed

The use statement with non-compound name 'File' has no effect

Thank you for your help !

Answer Source

Change use File; to use Illuminate\Support\Facades\File as File;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download