Delete MySQL User

Delete MySQL User

Deleting a MySQL user account is essential for database administrators to ensure the security and efficiency of the MySQL database server. The DROP USER statement deletes one or more MySQL accounts along with their associated grant table privileges. Only administrators with global CREATE USER or DELETE privileges can delete a user from MySQL.

1. Delete user

To remove a specific user, use the DROP USER statement as follows:

DROP USER myuser@localhost;

The hostname part (e.g., localhost) is optional. If omitted, MySQL defaults to the wildcard symbol (%), allowing the user to connect from any host.

DROP USER myuser;

It is equivalent to:

DROP USER myuser@'%';

2. Delete user if exists

If DROP USER is used on a non-existent user, MySQL will return an error. To avoid this error, include the IF EXISTS clause in the DROP USER statement:

DROP USER IF EXISTS myuser@localhost;

3. Delete multiple users

We can delete multiple users in a single command by separating them with commas:

DROP USER myuser@localhost, testuser;

Leave a Comment

Cancel reply

Your email address will not be published.