Bulk Delete ASP.NET Membership Users

Below is a SQL script that can be used to delete a list of users from an ASP.NET membership database.  It retrieves a list of users into a temporary table and then deletes the users from all the relevant tables in the database.

SELECT UserID, UserName
into #temp
FROM aspnet_Users
WHERE UserName in ('MEMBER10001','MEMBER10002','MEMBER10003','MEMBER10004','MEMBER10005')

-- Adjust the WHERE Clause to filter the users
-- for example WHERE UserName LIKE 'MEMBER%'
-- would delete all users whose username started with 'MEMBER'

DELETE FROM dbo.aspnet_Membership WHERE UserId IN (Select UserId from #temp)

DELETE FROM dbo.aspnet_UsersInRoles WHERE UserId IN (Select UserId from #temp)

DELETE FROM dbo.aspnet_Profile WHERE UserId IN (Select UserId from #temp)

DELETE FROM dbo.aspnet_PersonalizationPerUser WHERE UserId IN (Select UserId from #temp)

DELETE FROM dbo.aspnet_Users WHERE UserId IN (Select UserId from #temp)

Comments

2 responses to “Bulk Delete ASP.NET Membership Users”

  1. Baskar Avatar
    Baskar

    This is very helpful script. I cleaned up all old users from my FBA db using this script.

    Thanks for Chris Coulson…

  2. Peter Avatar
    Peter

    Brilliant,
    worked fine after changing some tablenames (had other Asp.Net Authentication tablenames).
    A clean solution i will use in the future for other uses too
    /Peter

Leave a Reply

Your email address will not be published. Required fields are marked *