How to Delete All Records Older Than 30 Days in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hi Guys,

In this quick example, let's see laravel delete all records older than 30 days. This example will help you how to delete all records older than 30 days in laravel. I’m going to show you about laravel delete records older than 30 days. This tutorial will give you a simple example of laravel delete old records 30 days example. So, let's follow a few steps to create an example of laravel delete all records older than 30 days.

If you want to keep only last 30 days records in your laravel application then i will give simple database query to delete all records older than 30 days in laravel. you need to just setup in your cron. so, let's see the simple query code.

Controller Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

Post::whereDate('created_at', '<=', now()->subDays(30))->delete();

}

}

I hope it can help you...

Tags :
Shares