Delete all users with applicationId = xx

Posted on 2009-12-22
Last Modified: 2013-11-07
how do remove all users where application id = xx

Question by:frosty1
    LVL 52

    Expert Comment

    by:Carl Tawn
    There is no out-of-the-box way to do it, so you would need to write your own routine for doing it. Something like the following should work:
    DECLARE @ApplicationName	NVARCHAR(256)
    SET @ApplicationName = N'TestApplication'
    SELECT @ApplicationID = [ApplicationId] FROM [aspnet_Applications] WHERE [ApplicationName] = @ApplicationName;
    	FOR SELECT [UserName] FROM [aspnet_Users] WHERE [ApplicationId] = @ApplicationID;
    DECLARE @UserName NVARCHAR(256);
    DECLARE @TableCount INT;
    OPEN UserCursor;
    FETCH NEXT FROM UserCursor INTO @UserName
    	EXEC [aspnet_Users_DeleteUser] @ApplicationName, @UserName, 8, @TableCount OUTPUT;
    	FETCH NEXT FROM UserCursor INTO @UserName
    CLOSE UserCursor;
    DEALLOCATE UserCursor; 

    Open in new window

    LVL 42

    Accepted Solution

    Deleting all users and related data from a site using ASP.NET authentication is fairly simple. Useful if copying a database and you want to remove all the users, but keep the structure and other data (e.g. user content) intact.

    Delete all users:
    foreach (MembershipUser u in Membership.GetAllUsers())
     Membership.DeleteUser(u.UserName, true);

    Author Comment

    thanks guys, actually both answered would do the trick. I selected that later as i'm comfortable in C# that SQL.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now