Search code examples
laraveleloquentcron

why aren't the entries deleting even if the condition meets in the where clause?


I got this code on the console Kernel.php file within the schedule function:

    $schedule->call(function () {
        User::where('deleted_at', '<=', Carbon::now())->delete();
    })->everyMinute(); 

It should delete all the users that have passed the deleted_at time variable, I launch the cron job in my local server with the command php artisan schedule:work since I have no server yet. The code executes every minute but nothing happens and there is no error. I tried with the simple now() but still nothing, I tried maybe setting the local time instead of the default UTC but also nothing


Solution

  • Soft deleted models do not show up with a regular Eloquent query, you need to use the withTrashed() or onlyTrashed() methods. If you're looking to permanently delete all the soft-deleted users, I'd suggest something like this, using the forceDelete() method:

    $schedule->call(fn() => User::onlyTrashed()->forceDelete())->everyMinute();