In SQL Server, you can use the sysmail_delete_account_sp
stored procedure to delete an existing Database Mail account.
You can delete an account by providing either its name or its ID (but you must provide either one or the other).
Example – Delete by Name
Here’s an example to demonstrate how to delete an account by specifying its name.
EXECUTE msdb.dbo.sysmail_delete_account_sp
@account_name = 'DB Admin';
Result:
Commands completed successfully.
This deleted the account called “DB Admin”.
The name is sysname.
Example – Delete by ID
As mentioned, you can alternatively provide the ID. To do this, change the @account_name
argument to @account_id
:
EXECUTE msdb.dbo.sysmail_delete_account_sp
@account_id = 1;
The ID is an int.
A Few Points
The sysmail_delete_account_sp
procedure deletes the account, regardless of whether the account is in use by a profile.
A profile that contains no accounts cannot successfully send email.
The sysmail_delete_account_sp
stored procedure is located in the msdb database and its owner is dbo. Therefore, you’ll need to provide three part naming if msdb isn’t the current database.