Adding a command to laravel deleting all unwanted users :-)

As spammer start flooding my users table with fake-accounts i startet to develop a command for laravel to run in a cron job for deleting all users which didnt verified their email within the last 24 hours.

A nice how-to do a laravel console command for cron can be found at: https://tutsforweb.com/how-to-set-up-task-scheduling-cron-job-in-laravel/

This is my routine to select all users which haven’t confirmed their email and their register date is older than 24 hours.

    public function handle()
    {
         $results = User::whereNull('email_verified_at')->get();
        $results = $results
            ->where('created_at', '<', Carbon::now()->subDays(1)->toDateTimeString());

        foreach ($results as $result)
        {
            $result->delete();
        }

    }

Added the following command to the crontab by crontab -e on the webserver to execute the command:

cd /var/www/vhosts/rolandradio.net/rolandradio2020 && /opt/plesk/php/7.4/bin/php artisan schedule:run >> /dev/null 2>&1

Leave a Reply

Your email address will not be published. Required fields are marked *