The Statement
DROP USER IF EXISTS 'old_app'@'localhost';
Run it as an administrator, in sudo mysql or sudo mariadb. It removes the account and all its privileges at once; no FLUSH PRIVILEGES is needed. IF EXISTS turns a missing account into a warning instead of error 1396, which suits scripts. The steps below come first, because there is no undo.
Step 1: Find the Exact Account
SELECT user, host FROM mysql.user WHERE user = 'old_app';
An account is a user name plus a host, so one name can have several rows, such as 'old_app'@'localhost' and 'old_app'@'10.0.0.5'. Each one is dropped on its own. If you leave out the host, as in DROP USER 'old_app';, the server takes 'old_app'@'%', which may not be the account you mean. The full list is explained in how to list users in MySQL.
Step 2: See What It Can Do
SHOW GRANTS FOR 'old_app'@'localhost';
The grants show which databases the account works on, and so which application may still depend on it. Check that application's configuration before you continue: after the drop, it fails at its next connection.
Step 3: Check Objects It Defines
Views, stored procedures, functions, triggers, and events run with the rights of their definer. If the definer account disappears, they stop working. Look for them first:
SELECT table_schema, table_name FROM information_schema.views
WHERE definer = 'old_app@localhost';
SELECT routine_schema, routine_name FROM information_schema.routines
WHERE definer = 'old_app@localhost';
SELECT trigger_schema, trigger_name FROM information_schema.triggers
WHERE definer = 'old_app@localhost';
SELECT event_schema, event_name FROM information_schema.events
WHERE definer = 'old_app@localhost';
If any rows come back, recreate those objects with another definer before you drop the account. MySQL 8.0.22 and later refuse to drop an account that is still a definer, unless the administrator holds a privilege that overrides the check, which turns the error into a warning. Do not rely on that check, and on MariaDB run these queries in any case.
Step 4: Lock It First (Optional)
ALTER USER 'old_app'@'localhost' ACCOUNT LOCK;
A locked account cannot open new connections but keeps its grants; sessions already open are not affected. Leave it locked for a few days: if nothing breaks, drop it; if something does, ACCOUNT UNLOCK restores it at once.
Step 5: Drop the Account
DROP USER IF EXISTS 'old_app'@'localhost';
DROP USER IF EXISTS 'old_app'@'localhost', 'old_app'@'10.0.0.5';
The second form removes several accounts in one statement.
What Stays Behind
- Databases and tables: dropping an account never removes data. Tables the user created stay where they are. To remove a database as well, take a dump first, as in how to create a database in MySQL.
- Open sessions:
DROP USERdoes not close connections that are already open; they keep working until they end. Find them withSHOW PROCESSLIST;and end them withKILLand the connection ID. - Linux users: database accounts are separate from system users; removing one does not touch the other.
Step 6: Confirm
SELECT user, host FROM mysql.user WHERE user = 'old_app';
An empty result means every account with that name is gone. If you need an account again later, see how to create a MySQL user.
Accounts Not to Drop
Leave the server's own accounts alone: mysql.sys, mysql.session, and mysql.infoschema on MySQL, mariadb.sys on MariaDB, and debian-sys-maint where it exists. Do not drop 'root'@'localhost' either; it is the account that recovers everything else.