{"id":1087,"date":"2023-09-30T14:30:53","date_gmt":"2023-09-30T14:30:53","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1087"},"modified":"2023-10-01T14:35:45","modified_gmt":"2023-10-01T14:35:45","slug":"automatically-deleting-30-day-old-data-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/automatically-deleting-30-day-old-data-in-laravel\/","title":{"rendered":"Automatically Deleting 30-Day Old Data in Laravel"},"content":{"rendered":"\n<p>Managing data in a Laravel application is a critical task to ensure optimal performance and prevent database clutter. In this guide, we&#8217;ll explore how to automatically delete data that is older than 30 days from your Laravel application&#8217;s database. We&#8217;ll use a Laravel controller and Eloquent to achieve this task efficiently.<\/p>\n\n\n\n<p><strong>Understanding the Code<\/strong><\/p>\n\n\n\n<p>The provided code snippet is a part of a Laravel controller named <code>PostController<\/code>. It contains a method named <code>index<\/code>, which is responsible for deleting data that is 30 days old.<\/p>\n\n\n\n<p>Here&#8217;s a breakdown of the code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Http\\Controllers\\Controller;\r\nuse Illuminate\\Http\\Request;\r\nuse App\\Models\\Post;\r\n<\/code><\/pre>\n\n\n\n<p>First, we import the necessary dependencies, including the <code>Post<\/code> model, which presumably represents the data we want to delete.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function index(Request $request)\r\n{\r\n    Post::whereDate('created_at', '&lt;=', now()->subDays(30))->delete();\r\n}\r\n<\/code><\/pre>\n\n\n\n<p>Inside the <code>index<\/code> method, we execute a database query using Eloquent. Here&#8217;s how it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We use the <code>Post<\/code> model to interact with the database.<\/li>\n\n\n\n<li>The <code>whereDate<\/code> method filters the records based on the <code>created_at<\/code> column of the <code>Post<\/code> model.<\/li>\n\n\n\n<li>We specify that we want to delete records where the <code>created_at<\/code> date is less than or equal to the current date minus 30 days, achieved using <code>now()-&gt;subDays(30)<\/code>.<\/li>\n\n\n\n<li>Finally, the <code>delete<\/code> method is called to remove the selected records from the database.<\/li>\n<\/ul>\n\n\n\n<p><strong>Implementing the Automatic Deletion Process<\/strong><\/p>\n\n\n\n<p>To make this code execute automatically, you can use Laravel&#8217;s task scheduling feature. Here&#8217;s how to set it up:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open the <code>app\/Console\/Kernel.php<\/code> file in your Laravel project.<\/li>\n\n\n\n<li>In the <code>schedule<\/code> method, add the following code to schedule the <code>index<\/code> method of <code>PostController<\/code> to run daily.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>protected function schedule(Schedule $schedule)\r\n{\r\n    $schedule->call(&#91;new \\App\\Http\\Controllers\\PostController, 'index'])->daily();\r\n}\r\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>Save the file.<\/li>\n<\/ol>\n\n\n\n<p>Now, Laravel will automatically run the <code>index<\/code> method of the <code>PostController<\/code> daily. This means that any data in the <code>Post<\/code> model&#8217;s table that is older than 30 days will be deleted automatically.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing data in a Laravel application is a critical task to ensure optimal performance and prevent database clutter. In this guide, we&#8217;ll explore how to automatically delete&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[399,398,206],"class_list":["post-1087","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-automatically-deleting","tag-automatically-deleting-30-day-old-data-in-laravel","tag-database"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1087"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1087\/revisions"}],"predecessor-version":[{"id":1088,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1087\/revisions\/1088"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}