DROP USER
In this page:
How to Drop a User
DROP USER permanently deletes an account that no longer needs any access to the database, removing both the login and every privilege that had been assigned to it.
Example: How to Drop a User
DROP USER 'app_user'@'localhost';
Dropping Multiple Users at Once
Multiple accounts can be dropped in a single statement by listing them separated by commas, which saves time when cleaning up several stale accounts at once rather than issuing separate commands.
Example: Dropping Multiple Users at Once
DROP USER 'app_user'@'localhost', 'temp_user'@'localhost';
The IF EXISTS Safety Check
Adding IF EXISTS prevents MySQL from throwing an error if you try to drop a user that's already gone, which keeps cleanup scripts from failing partway through when run more than once.
Example: The IF EXISTS Safety Check
DROP USER IF EXISTS 'app_user'@'localhost';
Dropping Users and Security Cleanup
After removing a user, reloading the privilege tables is recommended on production systems to make sure the account's access is fully revoked immediately rather than lingering in a cached state.
Example: Dropping Users and Security Cleanup
DROP USER 'app_user'@'localhost';
FLUSH PRIVILEGES;
Best Practices for Removing Users
Before dropping an account, searching the user list to confirm the exact username and host combination helps avoid the costly mistake of deleting the wrong account, especially when similar usernames exist.
Example: Best Practices for Removing Users
SELECT user, host FROM mysql.user WHERE user = 'app_user';
DROP USER 'app_user'@'localhost';
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: