Unable to delete the database user as the user is already logged in currently. error 15434

Sagar Pudi picture Sagar Pudi · Apr 28, 2015 · Viewed 41.5k times · Source

I have created the user sagar in SQL Server 2012 database and mapped it to the XYZ database. After some time I deleted the XYZ database and now I don't need the existing user sagar also. But when I try to delete the user from SQL Srever Management Studio. I am getting the following exception

Could not drop login 'sagar' as the user is currently logged in. error:15434

Answer

Sagar Pudi picture Sagar Pudi · Apr 28, 2015

User can be deleted after killing the session by identifying the session_id of the user.

SELECT session_id
FROM sys.dm_exec_sessions
WHERE login_name = 'sagar'

KILL 51  --51 is session_id here, you may get different id

Now you can delete the login simply by executing the below query (or) by using sql server management studio options.

DROP LOGIN 'sagar'