Laravel delete all rows older than 30 days

Created at 21-Oct-2021 , By samar

Laravel delete all rows older than 30 days

In this session, we will try our hand at solving the "Laravel delete all rows older than 30 days".

You can delete all rows from table which are 30 days older from current date. You can use whereDate() and whereRaw() SQL query method to delete the records from table.
  • Laravel eloquent to delete 30 days older records from today's date

    1. whereRaw() SQL Query to delete 30 days older records.
    $users = \App\Models\User::whereRaw('DATEDIFF(NOW(), created_at) > 30')->delete();
    dd($users);
    
    1. whereDate() method to delete 30 days older records from table.
    $users = \App\Models\User::whereDate('created_at', '<=', now()->subDays(30))->delete();
    dd($users);
    

    It will delete all the records which are 30 days older from today's date and return the number of records as output deleted from table. You can use any of them to get the 30 days older records from the particular table.

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.