Kill user session in Oracle

If you need to kill all the sessions for a particular user you can try using the following script.
It looks for all connections by AUSER (line 6) and then kills them.

SET SERVEROUTPUT ON SIZE 1000000
BEGIN
FOR usersession IN (SELECT s.USERNAME, s.OSUSER, s.STATUS, s.SID, s.SERIAL#
FROM   v$process p,v$session s
WHERE  p.addr = s.paddr
AND    s.username = 'AUSER')
LOOP
BEGIN
IF usersession.STATUS = 'ACTIVE' THEN
EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION ' || '''' || usersession.sid || ',' || usersession.serial# || '''' || 'IMMEDIATE';
ELSIF usersession.STATUS = 'INACTIVE' THEN
EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION ' || '''' || usersession.sid || ',' || usersession.serial# || '''' || 'IMMEDIATE';
ElSE
DBMS_OUTPUT.put_line (usersession.sid || ' : ' || usersession.serial# || ' : ' || usersession.OSUSER ||  ' : ' || usersession.STATUS);
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line(usersession.sid || ' : ' || usersession.serial#  ||  ' : ' || usersession.OSUSER);
END;
END LOOP;
END;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s