← Back to MySQL Course | Chapter 18: User Management & Security | Lesson 4 of 6

DROP USER

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

sql
DROP USER 'app_user'@'localhost';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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

sql
DROP USER 'app_user'@'localhost', 'temp_user'@'localhost';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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

sql
DROP USER IF EXISTS 'app_user'@'localhost';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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

sql
DROP USER 'app_user'@'localhost';
FLUSH PRIVILEGES;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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

sql
SELECT user, host FROM mysql.user WHERE user = 'app_user';
DROP USER 'app_user'@'localhost';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.