Tim

Footprints in the snow of a warped mind

How to: Remove users from the ASP.Net membership database

Where to find me

Flickr Icon  Twitter Icon  Linked In Icon  FaceBook Icon  Windows Live Alerts Butterfly  RSS 2.0 

Tag Cloud

AJAX (4) ASP (6) ASP.Net (47) Error Reporting (4) Web Service (1) WSDL (1) Atlas (2) Business (74) Business Start-up Advice (25) Client (13) Expanding Your Business (17) C# (15) Canoeing (4) Canoe Racing (5) Cheshire Ring Race (5) Racing (2) Training (4) CIMA (1) Cisco (1) 7970G (1) CSS (3) dasBlog (4) DDD (1) Design (9) Icons (1) Development (11) General (39) Christmas (6) Fun and Games (11) Internet (22) Random (46) RX-8 (8) Home Cinema (2) Hosting (1) IIS (8) iPhone (1) JavaScript (2) Marketing (5) Email (1) Multipack (1) Networking (2) Nintendo (1) OS Commerce (1) Photography (1) PHP (1) PowerShell (1) Press Release (1) Productivity (2) Security (2) SEO (5) Server Maintenance (3) Server Management (8) Social Networking (1) Experiment (1) Software (9) Office (5) Visual Studio (11) Windows (4) Vista (1) SQL (1) SQL Server (13) Stored Procedure (1) Testing (1) The Site Doctor (102) Turnover Challenge (1) Twitter (1) Umbraco (15) 2009 (1) Web Development (50) WebDD (33) Wii (1)

Blog Archive

Search

<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Recent Comments

Blog Archive

Various Links

Blogs I Read

[Feed] Google Blog
Official Google Webmaster Central Blog
[Feed] Matt Cutts
Gadgets, Google, and SEO
[Feed] Ol' Deano's Blog
My mate Dean's blog on my space, equally as random as mine but not off on as much of a tangent!
[Feed] Sam's Blog
Sam is one of my younger brothers studying Product Design and Manufacture at Loughborough, this is his blog :) Enjoy!

Recent Tracks

last.fm - The Social Music Revolution

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

newtelligence dasBlog 2.2.8279.16125

Send mail to the author(s) Email Me (Tim Gaunt)

© 2010 Tim Gaunt.

Sign In

    # Sunday, January 13, 2008

    How to: Remove users from the ASP.Net membership database

    Sunday, January 13, 2008 8:37:44 PM (GMT Standard Time, UTC+00:00)

    I'm sure I've blogged about this in the past -or perhaps it's just in my "to blog about list" but I thought I would share this little ditty on the Sunday night.

    If you ever need to delete a user from your ASP.Net Membership database this is a really useful SQL script to do just that (I often find that the ASP.Net web administration tool throws a SQL Exception while trying to delete a user).

    To use the code to delete a user from the ASP.Net membership database simple identify the Guid of the user and enter it where I've written 'THE GUID OF THE USER HERE' and hit go :)

    USE ASPNet
    GO

    DECLARE @UserId uniqueidentifier
    SET @UserId = 'THE GUID OF THE USER HERE'

    DELETE FROM aspnet_Profile WHERE UserID = @UserId
    DELETE FROM aspnet_UsersInRoles WHERE UserID = @UserId
    DELETE FROM aspnet_PersonalizationPerUser WHERE UserID = @UserId
    DELETE FROM dbo.aspnet_Membership WHERE UserID = @UserId
    DELETE FROM aspnet_users WHERE UserID = @UserId

    The message I was referring to above usually looks something like the following:

    Msg 547, Level 16, State 0, Line 9
    The DELETE statement conflicted with the REFERENCE constraint "FK__aspnet_Us__UserI__17036CC0". The conflict occurred in database "ASPNetMemberships", table "dbo.aspnet_UsersInRoles", column 'UserId'.
    The statement has been terminated.

    I've not looked into why it's happening (I expect it's something to do with an incorrect install on my behalf) but I'm sure there's a solution for it. I know there are a couple of built in SQL scripts i.e. aspnet_Users_DeleteUser but they required more params to get working ;)

    Tuesday, November 18, 2008 4:17:52 PM (GMT Standard Time, UTC+00:00)
    Actually, I think the error should be a question of order:

    BEGIN TRAN

    DELETE FROM aspnet_Membership WHERE UserID = @UserID
    DELETE FROM aspnet_Profile WHERE UserID = @UserID
    DELETE FROM aspnet_Users WHERE UserID = @UserID
    DELETE FROM aspnet_UsersInRoles WHERE UserID = @UserID

    ROLLBACK TRAN

    --COMMIT TRAN

    Unfortunately, the app I'm working on doesn't have PersonalizationPerUser, but I suppose that order can also be figured out with the above script. Of course, you'll want to comment out 'ROLLBACK TRAN' and uncomment 'COMMIT TRAN', once there are no errors.
    Wednesday, May 20, 2009 6:21:01 PM (GMT Daylight Time, UTC+01:00)
    What about the Membership.DeleteUser method? Clean and simple...
    John
    Thursday, May 21, 2009 9:05:13 AM (GMT Daylight Time, UTC+01:00)
    Useful thanks John however that doesn't help unless you boot up Visual Studio and write an application to delete the user...

    Sometimes I just need to delete a user from the db direct ;)
    Thursday, January 21, 2010 3:28:38 AM (GMT Standard Time, UTC+00:00)
    Tim,
    Thanks for this simple solution.

    Being only an occasional SQL user, was driving me mad how to delete 3 orphan uses.

    PG

    Peter Griffith
    Name
    E-mail
    (will show your gravatar icon)
    Home page

    Comment (HTML not allowed)  

    Live Comment Preview