Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devhoussam123's avatar

Send notification email when php artisan model:prune run

I can't get the notification email.

User.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\MassPrunable;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens;
    use HasFactory;
    use HasRoles;
    use MassPrunable;
    use Notifiable;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'avatar_url',
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];

    /**
     * Get the prunable model query.
     */
    public function prunable(): Builder
    {
        return static::onlyTrashed()->where('deleted_at', '<=', now()->subMinutes(5));
    }
}

app/Observers/UserObserver.php

<?php

namespace App\Observers;

use App\Models\User;
use App\Notifications\UserDeleted;

class UserObserver
{
    /**
     * Handle the User "created" event.
     */
    public function created(User $user): void
    {
        //
    }

    /**
     * Handle the User "updated" event.
     */
    public function updated(User $user): void
    {
        //
    }

    /**
     * Handle the User "deleted" event.
     */
    public function deleted(User $user): void
    {
        //
    }

    /**
     * Handle the User "restored" event.
     */
    public function restored(User $user): void
    {
        //
    }

    /**
     * Handle the User "force deleted" event.
     */
    public function forceDeleted(User $user): void
    {
        $user->notify(new UserDeleted());
    }
}

app/Notifications/UserDeleted.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;

class UserDeleted extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->subject(Lang::get('Account Permanently Deleted'))
            ->line(Lang::get('You are receiving this email because your account has been permanently deleted.'))
            ->line(Lang::get('Thank you for using our application!'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            //
        ];
    }
}
0 likes
16 replies
Snapey's avatar

you want to mail the user just deleted by sending using their user model ?

1 like
devhoussam123's avatar

@Snapey when I use php artisan model:prune all the removed account should get an email

1 like
Snapey's avatar

@devhoussam123 You cannot email using a deleted model !

Use this approach instead

    public function forceDeleted(User $user): void
    {
		Mail::to($user->email)->send(new UserDeleted();
    }
1 like
devhoussam123's avatar

@Snapey I have add

public function forceDeleted(User $user): void
  {
        // Send notification when the user account is force deleted.
        $user->notify(new UserForceDeleted());
  }

but when I run php artisan model:prune the model with deleted_at removed from database but notification not send even I setup everything correctly.

1 like
nexxai's avatar

@devhoussam123 How do you plan on sending an email to a user that has already been deleted from your database? They're gone. There's no user left to notify. You will need to send the notification before the deletion happens.

1 like
Snapey's avatar

@devhoussam123

did you try this?


    public function forceDeleted(User $user): void
    {
		Mail::to($user->email)->send(new UserDeleted();
    }

devhoussam123's avatar

@nexxai that's the case is there any thing i can do to sent an email notification before forceDelete

nexxai's avatar

@devhoussam123 I haven't checked, but if there's a forceDeleting() method in the observer, you could use that.

The idea is the methods that end in ing() happen before the action, and the methods that end in ed() happen after the action, so if you do it before the actual forceDelete happens, the user model should still exist and therefore you should be able to send an email to it.

Snapey's avatar

what have you done to investigate these last two days?

dd($user) in the forceDeleted event?

tried emailing a known email address and not the user?

kiwi0134's avatar

@devhoussam123 Nobody can help you if you do not follow any provided guides or suggestions and don't provide adequate feedback. Do not expect anyone to invest their time into trying to help you if you're not ready to properly cooperate.

Please or to participate in this conversation.