TechBuzzPro

How to Remove a Windows User Account Using C

· 5 min read · Windows & PC

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can remove a Windows user account programmatically from C by calling the NetUserDel function from the Netapi32.dll library. This is the same API the Windows UI uses behind the scenes, and it deletes the local user account and its profile data. Before you start, you need administrator rights, a C compiler (such as Visual Studio or MinGW), and a clear understanding that this is irreversible without a backup. The steps below show a minimal, working C program that removes a local account by name, plus the safety checks you should add.

What You'll Need

  • A Windows PC with a C compiler installed (Visual Studio with the Desktop development with C++ workload, or MinGW-w64).
  • Administrator privileges on the machine (the API call will fail with access denied otherwise).
  • The exact username of the local account you want to remove (case-insensitive, but must match).
  • Basic familiarity with compiling and running a console application.
  • A backup of any files in that user's profile that you need to keep, because the profile folder is not automatically deleted by NetUserDel.

Step-by-Step

  1. Open your C development environment. In Visual Studio, create a new Empty C++ project, add a .c file, and set the project to compile as C (or just use a .c extension). With MinGW, open a command prompt and use gcc directly.
  2. Include the required headers. At the top of your source file, add #include <windows.h> and #include <lm.h>. The second header declares NetUserDel and the NetApiBufferFree function you will need for error handling.
  3. Link the Netapi32 library. In Visual Studio, go to Project Properties > Linker > Input > Additional Dependencies and add netapi32.lib. With MinGW, add -lnetapi32 to your gcc command line.
  4. Write the main function. Declare a wide-string username variable, for example const wchar_t* username = L"OldUser";. Then call NET_API_STATUS status = NetUserDel(NULL, username);. Passing NULL as the server name targets the local machine.
  5. Check the return value. If status == NERR_Success, the account was deleted. Otherwise, print the numeric code. Common codes include ERROR_ACCESS_DENIED (5), NERR_UserNotFound (2221), and NERR_LastAdmin (2452). You can convert the code to a message with FormatMessage if you want readable output.
  6. Compile the program. In Visual Studio, build the solution. With MinGW, run gcc removeuser.c -o removeuser.exe -lnetapi32.
  7. Run as administrator. Right-click the resulting executable and choose Run as administrator, or open an elevated Command Prompt and launch it from there. Without elevation, the call returns ERROR_ACCESS_DENIED.
  8. Verify the removal. Open Settings > Accounts > Other users, or run net user in a command prompt, to confirm the account no longer appears. The user's profile folder under C:\Users may still exist; delete it manually if you no longer need the data.

Common Mistakes

  • Forgetting to link netapi32.lib. The code compiles but the linker fails with unresolved external symbol NetUserDel. Add the library as described above.
  • Running without elevation. The API returns access denied even for an administrator account if the process is not elevated. Always run the executable as administrator.
  • Using a narrow string. NetUserDel expects a wide string (LPCWSTR). Passing a char* will not compile or will produce garbage. Use the L prefix or MultiByteToWideChar.
  • Assuming the profile is deleted. NetUserDel removes the account from the local user database but leaves the C:\Users\<name> folder intact. Plan to clean it up separately.
  • Deleting the last administrator. Windows refuses to remove the last account with administrator rights; the call returns NERR_LastAdmin. Create or promote another admin first.
  • Not checking the return value. Silently ignoring the status means you may believe the account was removed when it was not.

Tips

  • Test the program on a disposable local account first, not on your own profile.
  • Use NetUserGetInfo before deletion to confirm the account exists and to log its details.
  • If you need to remove a domain account, pass the domain controller name instead of NULL, but only if you have the required permissions.
  • Wrap the call in a small function that returns a readable error string, so you can reuse it in scripts.
  • Remember that deleting an account does not delete files outside its profile, such as shared folders or registry keys created by applications.

Troubleshooting

If the program returns ERROR_ACCESS_DENIED, confirm you launched it elevated and that your account is a member of the Administrators group. If it returns NERR_UserNotFound, the username does not match any local account; check spelling and use net user to list exact names. If the linker complains about NetUserDel, the library is missing from the link step. If the account still appears in Settings after a successful return, sign out and back in, or restart the machine, because the UI can cache the list. If you get NERR_LastAdmin, create another administrator account before retrying. Finally, if the profile folder remains, delete it manually after confirming no processes are using it.

Frequently Asked Questions

Can I remove a Microsoft account this way?

NetUserDel removes the local account entry. If the account is linked to a Microsoft account, the local profile is removed but the Microsoft account itself is unaffected and can be signed in again.

Does this delete the user's files?

No. The profile folder under C:\Users remains. You must delete it manually if you want the files gone.

What permissions do I need?

You need to run the program as an administrator. The calling account must be a member of the local Administrators group.

Can I undo the deletion?

Not directly. You would need to recreate the account and restore any backed-up profile data. There is no built-in undo.

Why does NetUserDel return NERR_LastAdmin?

Windows prevents removal of the last account with administrator rights. Promote another account to administrator first.

Is there a command-line alternative?

Yes, net user <username> /delete performs the same operation from an elevated Command Prompt. The C API is useful when you need to integrate deletion into a larger program.