Can I force-delete a user?

In a site I am currently building, I implemented a user model (with a plugin), which ensures, that deleting a user does not actually delete them, but changes their template (to “customer-delete”) as a way to mark them a deletable and inactive.
This, of course, suppresses the standard action of deleting the user. When trying to delete again, a message is passed to the admin/editor—using the hook user.delete:before and checking for the user’s template—, that the user is already marked for deletion.

The actual deletion should be executed with a cronjob, after some time has passed.
My problem: The cronjob also gets the “mark as deletable” treatment and doesn’t execute.

Can I somehow pass a force parameter to the $user->delete() method? (As possible with $page->delete().)

This is my plugin code:

<?php

class CustomerUser extends User
{
    public function delete(): bool 
    {
	return $this->commit('delete', ['user' => $this], function ($user) {
            $user->changeRole('customer-delete');
            $update['updated'] = date('Y-m-d H:i:s');
            $user->update($update);
            return false;
        });
    }
}
class CustomerDeleteUser extends User
{
    public function delete(): bool 
    {
	return $this->commit('delete', ['user' => $this], function ($user) {
            return false;
        });
    }
}

Kirby::plugin('frwssr/user-models', [
    'userModels' => [
        'customer' => CustomerUser::class,
        'customer-delete' => CustomerDeleteUser::class,
    ],
    'hooks' => [
        'user.delete:before' => function ($user)
        {
            if ($user->role() == 'customer-delete'):
                throw new Exception('„' . $user->email() . '“ ist bereits zum Löschen vorgemerkt. (siehe Lösch-Hinweis)');
            endif;
        },
    ],
]);

I think the easiest would be to delete the user on the file system level in your cron job, instead of calling $user->delete().

1 Like

That simple, huh? :sweat_smile:
Thank you for the instant reply/help, Sonja! :+1: