Laravel Tutorials

Permanent Record Deletion with Laravel's forceDestroy

Published Updated
Permanent Record Deletion with Laravel's forceDestroy image

Laravel's soft deletes feature helps maintain data integrity by keeping deleted records in your database. However, there are times when you need to permanently remove these records. The new forceDestroy method simplifies this process, eliminating the need to first retrieve the model before permanent deletion.

This method is particularly useful when performing cleanup operations, managing user data for privacy compliance, or implementing moderation systems where certain records need to be completely removed from the database.

use App\Models\Post;
// Single record permanent deletion
Post::forceDestroy($id);
// Multiple records
Post::forceDestroy([$id1, $id2, $id3]);

Let's explore a practical example of a data cleanup service:

<?php
 
namespace App\Services;
 
use App\Models\User;
use App\Models\Content;
use Illuminate\Support\Facades\Log;
use App\Events\UserDataPurged;
 
class DataCleanupService
{
public function purgeInactiveUserData(int $monthsInactive = 12)
{
$inactiveUsers = User::onlyTrashed()
->where('deleted_at', '<=', now()->subMonths($monthsInactive))
->pluck('id');
 
if ($inactiveUsers->isEmpty()) {
return ['message' => 'No inactive users to purge'];
}
 
// Cleanup related content first
$contentCount = Content::onlyTrashed()
->whereIn('user_id', $inactiveUsers)
->count();
 
Content::whereIn('user_id', $inactiveUsers)
->forceDestroy();
// Permanently remove user accounts
$userCount = User::forceDestroy($inactiveUsers);
Log::info('Completed user data purge', [
'users_removed' => $userCount,
'content_removed' => $contentCount
]);
UserDataPurged::dispatch($inactiveUsers);
 
return [
'users_purged' => $userCount,
'content_purged' => $contentCount,
'message' => "Successfully purged {$userCount} inactive user accounts"
];
}
}

The forceDestroy method streamlines permanent deletion operations, making your code cleaner and more efficient when managing soft-deleted records.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article