DeleteRole Method (String)
Collapse the table of content
Expand the table of content

Roles.DeleteRole Method (String)


Removes a role from the data source.

Namespace:   System.Web.Security
Assembly:  System.Web (in System.Web.dll)

public static bool DeleteRole(
	string roleName


Type: System.String

The name of the role to delete.

Return Value

Type: System.Boolean

true if roleName was deleted from the data source; otherwise, false.

Exception Condition

roleName is null.


roleName is an empty string or contains a comma (,).


roleName has one or more members.


Role management is not enabled.

The DeleteRole method removes a role name from the data source. DeleteRole calls the DeleteRole method of the default role provider to remove the specified role from the data source.

If the role identified by the roleName parameter has one or more members, then an exception will be thrown and the role will not be deleted.

The following code example deletes a role from the data source. For an example of a Web.config file that enables role management, see Roles.

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

<%@ Import Namespace="System.Web.Security" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  string[] rolesArray;

  public void Page_Load(object sender, EventArgs args)
    if (!IsPostBack)
      // Bind roles to ListBox.

      rolesArray = Roles.GetAllRoles();
      RolesListBox.DataSource = rolesArray;

  public void DeleteRole_OnClick(object sender, EventArgs args)
    string delRole = "";

      delRole = RolesListBox.SelectedItem.Value;


      Msg.Text = "Role '" + Server.HtmlEncode(delRole) + "' deleted.";

      // Re-bind roles to ListBox.

      rolesArray = Roles.GetAllRoles();
      RolesListBox.DataSource = rolesArray;
      Msg.Text = "Role '" + Server.HtmlEncode(delRole) + "' <u>not</u> deleted.";



<html xmlns="" >
  <title>Sample: Delete Role</title>
  <form runat="server" id="PageForm">
      Delete Role</h3>
    <asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
    <table border="0">
        <td valign="top">
          Delete Role:</td>
        <td valign="top">
          <asp:ListBox ID="RolesListBox" runat="server" Rows="8" /></td>
        <td valign="top">
          <asp:Button Text="Delete Role" ID="DeleteRoleButton" runat="server" OnClick="DeleteRole_OnClick" /></td>

.NET Framework
Available since 2.0
Return to top
© 2015 Microsoft