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 →
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article
Black Friday: Learn the habits behind AI workflows that work image

Black Friday: Learn the habits behind AI workflows that work

Read article
Laravel Black Friday and Cyber Monday Deals 2026 image

Laravel Black Friday and Cyber Monday Deals 2026

Read article
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article