diff --git a/Jellyfin.Server.Implementations/Users/UserManager.cs b/Jellyfin.Server.Implementations/Users/UserManager.cs
index 9041147588..d86b25f81e 100644
--- a/Jellyfin.Server.Implementations/Users/UserManager.cs
+++ b/Jellyfin.Server.Implementations/Users/UserManager.cs
@@ -192,15 +192,15 @@ namespace Jellyfin.Server.Implementations.Users
}
///
- public void DeleteUser(User user)
+ public void DeleteUser(Guid userId)
{
+ var dbContext = _dbProvider.CreateContext();
+ var user = dbContext.Users.Find(userId);
if (user == null)
{
- throw new ArgumentNullException(nameof(user));
+ throw new ArgumentNullException(nameof(userId));
}
- var dbContext = _dbProvider.CreateContext();
-
if (dbContext.Users.Find(user.Id) == null)
{
throw new ArgumentException(string.Format(
@@ -226,9 +226,18 @@ namespace Jellyfin.Server.Implementations.Users
CultureInfo.InvariantCulture,
"The user '{0}' cannot be deleted because there must be at least one admin user in the system.",
user.Username),
- nameof(user));
+ nameof(userId));
+ }
+
+ // Clear all entities related to the user from the database.
+ if (user.ProfileImage != null)
+ {
+ dbContext.Remove(user.ProfileImage);
}
+ dbContext.RemoveRange(user.Permissions);
+ dbContext.RemoveRange(user.Preferences);
+ dbContext.RemoveRange(user.AccessSchedules);
dbContext.Users.Remove(user);
dbContext.SaveChanges();
OnUserDeleted?.Invoke(this, new GenericEventArgs(user));
diff --git a/MediaBrowser.Api/UserService.cs b/MediaBrowser.Api/UserService.cs
index 131def5542..6e9d788dc1 100644
--- a/MediaBrowser.Api/UserService.cs
+++ b/MediaBrowser.Api/UserService.cs
@@ -365,15 +365,8 @@ namespace MediaBrowser.Api
public Task DeleteAsync(DeleteUser request)
{
- var user = _userManager.GetUserById(request.Id);
-
- if (user == null)
- {
- throw new ResourceNotFoundException("User not found");
- }
-
- _sessionMananger.RevokeUserTokens(user.Id, null);
- _userManager.DeleteUser(user);
+ _userManager.DeleteUser(request.Id);
+ _sessionMananger.RevokeUserTokens(request.Id, null);
return Task.CompletedTask;
}
diff --git a/MediaBrowser.Controller/Library/IUserManager.cs b/MediaBrowser.Controller/Library/IUserManager.cs
index fe3e4f9e68..4ff3cee0f0 100644
--- a/MediaBrowser.Controller/Library/IUserManager.cs
+++ b/MediaBrowser.Controller/Library/IUserManager.cs
@@ -111,8 +111,8 @@ namespace MediaBrowser.Controller.Library
///
/// Deletes the specified user.
///
- /// The user to be deleted.
- void DeleteUser(User user);
+ /// The if of the user to be deleted.
+ void DeleteUser(Guid userId);
///
/// Resets the password.