Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-30 · container image gvenzl/oracle-free:23-slim
ORA-01940 appears when you run DROP USER while that user is logged in somewhere: an application connection pool, a SQL*Plus window, a tool left open. The session does not have to be doing anything; an idle (INACTIVE) session is enough.
Below is the case reproduced on Oracle 26ai Free with two sessions, and the fix that was actually run.
01The user still has an open session
Session A logged in as lab_b and ran a query. While it stayed connected, session B (system) ran DROP USER lab_b CASCADE and got ORA-01940.
The fix was run from session B while A was still connected: v$session showed one LAB_B session (status INACTIVE, program sqlplus). ALTER USER lab_b ACCOUNT LOCK stopped new logins, a PL/SQL loop ran ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE for every LAB_B session, and then DROP USER lab_b CASCADE returned User dropped. A check on dba_users returned 0.
Setup
CREATE TABLE app_log (id NUMBER, msg VARCHAR2(50));
INSERT INTO app_log VALUES (1, 'start');
COMMIT;Table created.
1 row created.
Commit complete.SELECT COUNT(*) FROM app_log; COUNT(*)
----------
1
1 row selected.DROP USER lab_b CASCADE;DROP USER lab_b CASCADE
*
ERROR at line 1:
ORA-01940: cannot drop a user who is currently connected
Help: https://docs.oracle.com/error-help/db/ora-01940/Lock the account first. If you only kill sessions, a connection pool can log in again before DROP USER runs. Killing a session rolls back its uncommitted work, so check who it is before you kill it.
SELECT COUNT(*) FROM app_log; COUNT(*)
----------
1
1 row selected.COLUMN machine FORMAT a20
COLUMN program FORMAT a30
SELECT sid, serial#, status, program FROM v$session WHERE username = 'LAB_B'; SID SERIAL# STATUS PROGRAM
---------- ---------- ------------------------------ ------------------------------
60 950 INACTIVE sqlplus@bd782006fa71 (TNS V1-V
3)
1 row selected.ALTER USER lab_b ACCOUNT LOCK;
BEGIN
FOR s IN (SELECT sid, serial# sr FROM v$session WHERE username = 'LAB_B') LOOP
EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION ''' || s.sid || ',' || s.sr || ''' IMMEDIATE';
END LOOP;
END;
/User altered.
PL/SQL procedure successfully completed.DROP USER lab_b CASCADE;
SELECT COUNT(*) FROM dba_users WHERE username = 'LAB_B';User dropped.
COUNT(*)
----------
0
1 row selected.FAQ
How do I find which sessions block DROP USER?
Query v$session with WHERE username = 'NAME' (upper case). The output shows sid, serial#, status and program; INACTIVE sessions also count.
How do I drop a user that is connected in Oracle?
Lock the account with ALTER USER ... ACCOUNT LOCK, kill each session with ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE, then run DROP USER ... CASCADE. That order worked here.
Why lock the account before killing sessions?
So nothing can log in again between the kill and the DROP. An application pool usually reconnects on its own.
Does CASCADE avoid ORA-01940?
No. In this test DROP USER lab_b CASCADE itself failed with ORA-01940. CASCADE is about the user's objects, not sessions.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0