| Lesson 13 | Dropping a User |
| Objective | Delete users and their data from Oracle AI Database 26ai |
DROP USER coin_admin; removes an empty user; DROP USER coin_admin CASCADE; also removes that user's schema objects and data. Use ACCOUNT LOCK for temporary suspension. DROP USER is DDL and cannot be undone with ROLLBACK.
You need the DROP USER system privilege. The examples concern a local user: connect to the correct PDB and verify SYS_CONTEXT('USERENV', 'CON_NAME'). Do not apply them to Oracle-maintained accounts. Common users require separate root/container planning.
From time to time, it becomes necessary to remove a database user (schema). Common reasons include offboarding an employee, decommissioning an application schema, or cleaning up test accounts.
The statement used to remove a user is DROP USER.
Whether the drop succeeds depends on whether that user owns objects (tables, views, procedures, sequences, and so on).
DROP USER [IF EXISTS] username [CASCADE];
CASCADE: Oracle will only drop the user if they do not own any schema objects.
This protects you from accidentally deleting a schema that contains data.
CASCADE: Oracle drops the user and also drops the objects owned by that user, along with
some, but not all, effects on objects in other schemas that reference them, covered in detail below.
IF EXISTS: the statement completes quietly rather than raising an error if the named user
has already been removed, useful for cleanup scripts that may run more than once against the same environment.
One restriction worth knowing before you attempt any drop: a user's schema containing a table tracked by a flashback data archive cannot be dropped until that table's flashback data archive tracking is disabled first.
A connected user cannot be dropped. Check sessions first; CASCADE does not bypass this restriction. Coordinate application shutdown and session disconnection before retrying. Locking blocks new logins but does not disconnect sessions already running.
SELECT sid, serial#, status
FROM v$session
WHERE username = 'COIN_ADMIN';
This checks the local instance. In RAC, inspect GV$SESSION, including INST_ID, across instances. These administrative views require appropriate access.
If the user owns no objects, you can drop them directly:
SQL> DROP USER COIN_ADMIN;
User dropped.
If the user owns schema objects, Oracle prevents a plain drop and requires you to be explicit.
The exact error message can vary, but the meaning is consistent: the user owns objects, so you must specify
CASCADE to delete the schema and its data.
SQL> DROP USER COIN_ADMIN;
DROP USER COIN_ADMIN
*
ERROR at line 1:
ORA-01922: CASCADE must be specified to drop 'COIN_ADMIN'
Cause: the user owns objects that must be dropped along with the user.
Action: use DROP USER username CASCADE; if you intend to drop the user and everything
they own. Worth keeping distinct: privileges granted to the user are not the same thing as objects
owned by the user, and only the latter triggers this error.
To intentionally remove the user and everything they own:
SQL> DROP USER COIN_ADMIN CASCADE;
User dropped.
Dropping a user also purges that user's recycle-bin objects. The SQL*Plus transcripts above are illustrative, not execution results from this lesson. Operational note: dropping a schema can be a high-impact change. In production, confirm you have a current backup and that the schema is not required by any application, job, or integration.
CASCADE's effects on the dropped user's own schema are thorough: tables, views, indexes, sequences, triggers, and PL/SQL objects the user owns are all removed. Its effects on objects in other schemas are more specific, and in one important respect more limited, than "everything related gets cleaned up." This is worth knowing precisely before you rely on CASCADE for anything beyond a trivial, isolated schema:
If you are unsure why a user cannot be dropped, or you want to review impact before using CASCADE,
two separate questions are worth asking, since one query does not answer both of them.
A practical starting point is DBA_OBJECTS
(use an authorized DBA for a complete review; ALL_OBJECTS only shows accessible objects), which inventories many objects CASCADE will remove
from the target user's own schema:
SELECT object_type, COUNT(*) AS object_count
FROM dba_objects
WHERE owner = 'COIN_ADMIN'
GROUP BY object_type
ORDER BY object_type;
SELECT object_name, object_type
FROM dba_objects
WHERE owner = 'COIN_ADMIN'
ORDER BY object_type, object_name;
One caution worth taking seriously: if this query comes back empty, or nearly empty, but Oracle still refuses a
plain DROP USER with ORA-01922, do not assume the error is wrong. Not every schema-owned item is
guaranteed to show up cleanly in a general DBA_OBJECTS scan the way an ordinary table or view does. If you hit this
situation, check specialized views for the specific object types the schema might hold, such as
DBA_SCHEDULER_JOBS for scheduled jobs or DBA_QUEUES for Advanced Queuing queues, rather
than concluding the schema is genuinely empty just because DBA_OBJECTS looks that way.
This is a genuinely different question, and DBA_OBJECTS does not answer it. Two more targeted queries cover the two distinct risks covered above.
To find views, synonyms, and PL/SQL objects in other schemas that will be invalidated rather than dropped:
SELECT owner, name, type
FROM dba_dependencies
WHERE referenced_owner = 'COIN_ADMIN'
AND owner <> 'COIN_ADMIN';
To find foreign key constraints in other schemas that will be silently dropped along with this user's tables, note this requires a different view entirely, since DBA_DEPENDENCIES does not track table-to-table foreign key relationships at all:
SELECT c.owner, c.constraint_name, c.table_name
FROM dba_constraints c
JOIN dba_constraints p
ON p.owner = c.r_owner
AND p.constraint_name = c.r_constraint_name
WHERE c.constraint_type = 'R'
AND p.owner = 'COIN_ADMIN'
AND c.owner <> 'COIN_ADMIN';
These queries provide a starting inventory of what happens when you execute
DROP USER COIN_ADMIN CASCADE: what disappears entirely, what gets invalidated elsewhere, and what
foreign keys elsewhere get dropped. Matching both referenced owner and constraint name avoids false matches across schemas. Also review application SQL, dynamic SQL, jobs, and integrations: dictionary dependencies do not provide a complete operational impact inventory.
Click the quiz link below to check your understanding of user management.
User Management - QuizOracle AI Database 26ai reference: DROP USER.