How-to

Remove a MySQL User Safely

DROP USER removes an account at once. The care goes into what happens first: finding every host variant, ending sessions and fixing objects that run as that user.

What is the command?

DROP USER 'legacy_api'@'203.0.113.40';

MySQL and MariaDB both remove the account and all of its privileges in one step. You do not need to REVOKE first or run FLUSH PRIVILEGES afterwards. DROP USER IF EXISTS avoids an error in scripts when the account is already gone.

Why do I need to look for host variants?

An account is a name plus a host, so legacy_api may exist several times: once for localhost, once for an address, once for %. Dropping one leaves the others working. List them all:

SELECT user, host FROM mysql.user WHERE user = 'legacy_api';

Then drop each pair explicitly. Several at once is allowed: DROP USER 'legacy_api'@'localhost', 'legacy_api'@'%';.

Should I lock the account before deleting it?

Yes, when you are not completely sure nothing uses it. Locking is reversible; dropping is not:

ALTER USER 'legacy_api'@'203.0.113.40' ACCOUNT LOCK;
-- a week later, if nothing complained:
DROP USER 'legacy_api'@'203.0.113.40';

Watch the application logs and the server's error log during that week. A forgotten cron job or reporting tool will show up as failed logins, and ALTER USER ... ACCOUNT UNLOCK restores it instantly.

What are definers and why do they matter?

Views, stored procedures, functions, triggers and events can run with the rights of the account that defined them, recorded as DEFINER. Drop that account and those objects fail with errors such as "The user specified as a definer does not exist". Find them first:

SELECT 'view' AS kind, table_schema, table_name AS name FROM information_schema.views WHERE definer LIKE 'legacy_api@%'
UNION ALL SELECT routine_type, routine_schema, routine_name FROM information_schema.routines WHERE definer LIKE 'legacy_api@%'
UNION ALL SELECT 'trigger', trigger_schema, trigger_name FROM information_schema.triggers WHERE definer LIKE 'legacy_api@%'
UNION ALL SELECT 'event', event_schema, event_name FROM information_schema.events WHERE definer LIKE 'legacy_api@%';

Recreate each with a new definer, for example ALTER DEFINER = 'app_owner'@'localhost' VIEW ... for views, or drop and recreate routines with the new definer. MySQL 8.0.22 and later warn or refuse when you drop an account that is a definer, depending on settings; MariaDB does not check.

How do I end its sessions?

Dropping an account does not close its open connections; they keep running until they disconnect. Kill them:

SELECT id, host, db, command, time FROM information_schema.processlist WHERE user = 'legacy_api';
KILL 48213;

Use the ids from the first query. Details on killing queries versus connections are in Kill a MySQL query or connection.

What about a whole team leaving at once?

GoalStatement
Disable a person's access nowALTER USER 'nia'@'%' ACCOUNT LOCK;
Rotate an app password insteadALTER USER 'shop'@'localhost' IDENTIFIED BY 'new-secret';
Remove one right onlyREVOKE DELETE ON shop.* FROM 'nia'@'%';
Remove a role from someoneREVOKE 'app_write' FROM 'nia'@'%';
Delete the accountDROP USER 'nia'@'%';

MySQL 8 also supports dual passwords with RETAIN CURRENT PASSWORD, so an application password can be rotated without a moment where old clients fail.

Does dropping a user delete its data?

No. Databases and tables do not belong to MySQL accounts the way PostgreSQL objects belong to roles. The data stays; only the login and its grants disappear. To remove a database as well, run DROP DATABASE separately, after a dump with mysqldump --single-transaction.

How do I confirm it is gone?

SELECT user, host FROM mysql.user WHERE user = 'legacy_api';
SHOW GRANTS FOR 'legacy_api'@'203.0.113.40';

The first should return nothing; the second should fail with "There is no such grant defined". If the account had firewall rules or bind-address changes made for it, remove those too. Creating accounts properly is covered in MySQL users and privileges.

Keep Reading

MySQL users and privileges

Create MySQL 8 accounts with caching_sha2 passwords, least-privilege grants, roles, lockout rules and dynamic privileges for admin tasks.

Open

Kill a MySQL query or connection

Find runaway MySQL or MariaDB queries in the processlist, end the query or the whole connection, and set time limits so they stop themselves.

Open

Delete a PostgreSQL user

Remove a PostgreSQL role cleanly: end its sessions, hand its objects over, drop leftover privileges in every database, then drop the role.

Open

MySQL servers

Give InnoDB a buffer pool that holds your working set, NVMe for the redo log and no hypervisor between query and disk.

Open

FAQ

Quick Answers

Run DROP USER 'name'@'host' for each host entry of that name.

No. DROP USER takes effect immediately.

No. Data stays; only the account and its grants are removed.

A view, routine, trigger or event was defined by a dropped account. Recreate it with a new DEFINER.

Yes. ALTER USER with ACCOUNT LOCK blocks logins until you unlock it.