{"record":{"id":"844931dfe54616b8","repo":"nopSolutions/nopCommerce","slug":"you-cannot-delete-this-email-account-at-least-one","errorCode":null,"errorMessage":"You cannot delete this email account. At least one account is required.","messagePattern":"You cannot delete this email account\\. At least one account is required\\.","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"warning","filePath":"src/Libraries/Nop.Services/Messages/EmailAccountService.cs","lineNumber":99,"sourceCode":"        emailAccount.DisplayName = CommonHelper.EnsureMaximumLength(emailAccount.DisplayName, 255);\r\n        emailAccount.Host = CommonHelper.EnsureMaximumLength(emailAccount.Host, 255);\r\n        emailAccount.Username = CommonHelper.EnsureMaximumLength(emailAccount.Username, 255);\r\n        emailAccount.Password = CommonHelper.EnsureMaximumLength(emailAccount.Password, 255);\r\n\r\n        await _emailAccountRepository.UpdateAsync(emailAccount);\r\n    }\r\n\r\n    /// <summary>\r\n    /// Deletes an email account\r\n    /// </summary>\r\n    /// <param name=\"emailAccount\">Email account</param>\r\n    /// <returns>A task that represents the asynchronous operation</returns>\r\n    public virtual async Task DeleteEmailAccountAsync(EmailAccount emailAccount)\r\n    {\r\n        ArgumentNullException.ThrowIfNull(emailAccount);\r\n\r\n        if ((await GetAllEmailAccountsAsync()).Count == 1)\r\n            throw new NopException(\"You cannot delete this email account. At least one account is required.\");\r\n\r\n        await _emailAccountRepository.DeleteAsync(emailAccount);\r\n    }\r\n\r\n    /// <summary>\r\n    /// Gets an email account by identifier\r\n    /// </summary>\r\n    /// <param name=\"emailAccountId\">The email account identifier</param>\r\n    /// <returns>\r\n    /// A task that represents the asynchronous operation\r\n    /// The task result contains the email account\r\n    /// </returns>\r\n    public virtual async Task<EmailAccount> GetEmailAccountByIdAsync(int emailAccountId)\r\n    {\r\n        return await _emailAccountRepository.GetByIdAsync(emailAccountId, cache => default);\r\n    }\r\n\r\n    /// <summary>\r","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Messages/EmailAccountService.cs#L81-L117","documentation":"Thrown by EmailAccountService.DeleteEmailAccountAsync when the account being deleted is the only remaining one (GetAllEmailAccountsAsync().Count == 1). nopCommerce requires at least one email account because message templates and settings reference a default account; deleting the last one would leave dangling references and break email sending. The exception is a NopException surfaced to the admin UI.","triggerScenarios":"Calling DeleteEmailAccountAsync on the last remaining EmailAccount; an admin attempting to delete the only configured email account.","commonSituations":"Admin cleaning up test accounts and deleting the last one; automation script that removes all email accounts; attempt to delete before creating a replacement.","solutions":["Create at least one other EmailAccount before deleting the current last one.","In admin, add a new account first, then delete the unwanted one.","Guard UI/scripts: disable the delete action when the account list has a single entry."],"exampleFix":"// before\nawait _emailAccountService.DeleteEmailAccountAsync(account);\n\n// after - ensure more than one account exists before deleting\nif ((await _emailAccountService.GetAllEmailAccountsAsync()).Count <= 1)\n    return BadRequest(\"Create another email account before deleting this one.\");\nawait _emailAccountService.DeleteEmailAccountAsync(account);","handlingStrategy":"validation","validationCode":"// Block deletion of the last email account before calling the service\nvar accounts = await _emailAccountService.GetAllEmailAccountsAsync();\nif (accounts.Count <= 1)\n    return BadRequest(\"Create another email account before deleting this one.\");\nawait _emailAccountService.DeleteEmailAccountAsync(account);","typeGuard":"static async Task<bool> CanDeleteAsync(IEmailAccountService svc)\n    => (await svc.GetAllEmailAccountsAsync()).Count > 1;","tryCatchPattern":"try { await _emailAccountService.DeleteEmailAccountAsync(account); }\ncatch (NopException ex) when (ex.Message.Contains(\"At least one account is required\"))\n{ /* inform the user to create a replacement account first */ }","preventionTips":["Create a replacement account before deleting the last one.","Disable the delete control in the UI when only one account remains.","Never bulk-delete email accounts down to zero."],"tags":["email-account","validation","admin","nopcommerce"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}