How to delete a user from membership in c#

Delete a user from membership
.Net framework Membership.DeleteUser() method allow us to delete a user and any related user data from the database. Membership class DeleteUser() method exists in System.Web.Security namespace.

DeleteUser(String) method require to pass a parameter named 'username'. this 'username' parameter value data type is System.String which represents the name of the user to delete.

DeleteUser(String) method return value data type is System.Boolean. method return 'true', if the user deleted; otherwise it return 'false'.

DeleteUser(String) method throw ArgumentException exception, if the 'username' is an empty string or contains a comma. method also throw ArgumentNullException, if the 'username' is null.

the deleted user data stored in the database for the roles, profile or web parts personalization is also deleted when we are using the SqlRoleProvide, SqlProfileProvider and SqlPersonalizationProvider objects for data storage.

.Net framework Membership.DeleteUser(String, Boolean) overloaded method also allow us to delete a user from the database. this method 'deleteAllRelatedData' parameter value data type is System.Boolean. if we set this parameter value to 'true', then method will also delete user related data from database. 'false' value indicate method leave data related to the user in the database.

the following c# example code demonstrate us how can we delete a user from membership programmatically at run time in an application.

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
    protected void Button1_Click(object sender, System.EventArgs e) {
        Label1.Text = "User deleted successfully!";

    protected void DropDownListDataBind() {
        DropDownList1.DataSource = Membership.GetAllUsers();

<html xmlns="">
<head runat="server">
    <title>DeleteUser method example: how to delete a user in</title>
    <form id="form1" runat="server">
        <h2>DeleteUser method example</h2>
        <asp:Label ID="Label1" runat="server" Font-Bold="true" ForeColor="DarkCyan"></asp:Label>
        <br /><br />
        <b>Select user</b>
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" BackColor="AliceBlue"></asp:DropDownList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1" Text="*"></asp:RequiredFieldValidator>
        <br /><br />
        <asp:Button ID="Button1" runat="server" Text="Delete selected user" OnClick="Button1_Click" />

Related examples