SyntaxHighlighter JS

2017-07-30

Forcing Oracle to drop user

If there are sessions currently connected to an Oracle user schema, then you cannot drop the user. For example, if the Oracle user 'mytest' is currently logged in, then

DROP USER mytest CASCADE;

ORA-01940: cannot drop a user that is currently connected 

If you cannot get the Oracle user to voluntarily log off, then as the Oracle admin, you will have to kill the Oracle user session.

There are two ways to kill an Oracle session

ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;
ALTER SYSTEM DISCONNECT SESSION 'sid,serial#' IMMEDIATE;

With "kill session", Oracle will request the session to end. With "disconnect session", Oracle will terminate the server process associated with the session.

The Oracle script at https://github.com/juttayaya/oracle/blob/master/drop-user/drop_user_force.sql will both kill and disconnect all Oracle sessions for a user then drop it.  Example of usage:

sqlplus sys as sysdba @drop_user_force.sql mytest

If you are using AWS Oracle RDS, then use the equivalent script https://github.com/juttayaya/oracle/blob/master/drop-user/drop_user_force_aws_rds.sql . Example of usage:

sqlplus sys as sysdba@aws-rds-hostname:1521/ORCL @drop_user_force_aws_rds.sql mytest

No comments:

Post a Comment