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

Jeyziii's avatar

How to handle deleting user with soft delete

When I create an account then tried to delete it, since it just soft deletes it, I cannot register the same credentials. Should I remove soft delete or there are ways to solve it while not removing the soft delete? I am using laravel jetstream and laravel socialite. TIA.

0 likes
7 replies
LaryAI's avatar
Level 58

There are a few ways to handle this situation while still using soft deletes on the users table. One option is to add a unique constraint to the email or username column, so that even if a user is soft deleted, their email or username cannot be used again for a new account. Another option is to add a "deleted_at" column to the unique constraint, so that the constraint only applies to non-deleted users.

Here's an example of how to add a unique constraint with a "deleted_at" column in a Laravel migration:

Schema::table('users', function (Blueprint $table) {
    $table->unique(['email', 'deleted_at']);
});

This will ensure that no two non-deleted users can have the same email address, but a deleted user's email address can be used again for a new account.

Alternatively, you could also consider using a "soft delete cascade" to delete related records when a user is soft deleted. For example, if a user has associated records in a "posts" table, you could set up a cascade so that when the user is soft deleted, all of their posts are also soft deleted. This would prevent the issue of a deleted user's email or username being used again for a new account.

Here's an example of how to set up a soft delete cascade in a Laravel migration:

Schema::table('posts', function (Blueprint $table) {
    $table->foreign('user_id')
          ->references('id')
          ->on('users')
          ->onDelete('cascade');
});

This will ensure that when a user is soft deleted, all of their associated posts will also be soft deleted.

Ultimately, whether or not to use soft deletes on the users table depends on your specific use case and requirements. If you need to be able to reuse email or username credentials, then soft deletes may not be the best option. However, if you want to be able to restore deleted users or keep a record of when users were deleted, then soft deletes can be a useful feature.

tisuchi's avatar

@jeyziii During registration, you still allow users to be joined only when the user exists and deleted_at is not empty.

1 like
Jeyziii's avatar

@tisuchi I think my question is confusing, sorry. My problem is when I register a new account then I delete it. Since it is soft deleted, the deleted_at will have a value right? it will not be really deleted in the database. Now, when I register again the same credentials, it does not let me register since the data is still in the database. What should I do? I want users to let them register again after they deleted the account.

AddWebContribution's avatar

@jeyziii If you're using soft deletes in your database, then you're correct that the deleted record will still exist in the database with a non-null deleted_at value. This means that if a user tries to register with the same credentials, they will be prevented from doing so because the record still exists in the database.

do one thing while registration check whether the credential are in soft delete it or not if yes then register them with that credential and deleted would come to null

$user = User::onlyTrashed()
    ->where('email', $email)
    ->first();

it this exits then restore the user data

	$user->restore();
1 like
Jeyziii's avatar

@saurabhd This is what I did

public function create(array $input): User
{   
$deletedUser = User::onlyTrashed()->where('email', $input['email'])->first();
if(!$deletedUser){
Validator::make($input, [
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
])->validate();

return User::create([
'email' => $input['email'],
'password' => Hash::make($input['password']),
]);

}else{
$deletedUser->restore();
}
}

I get an error App\Actions\Fortify\CreateNewUser::create(): Return value must be of type App\Models\User, none returned

newbie360's avatar

@Jeyziii very danger !!!!

}else{
	$deletedUser->restore();
}

a person can re-avtive other user deleted account, if he guess a valid email

1 like

Please or to participate in this conversation.