Skip to content

Instantly share code, notes, and snippets.

@oehme
Created February 8, 2013 06:42
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oehme/4737130 to your computer and use it in GitHub Desktop.
Save oehme/4737130 to your computer and use it in GitHub Desktop.
Force drop user in Oracle database
DECLARE
open_count integer;
BEGIN
-- prevent any further connections
EXECUTE IMMEDIATE 'alter user @USERNAME account lock';
--kill all sessions
FOR session IN (SELECT sid, serial#
FROM v$session
WHERE username = '@USERNAME')
LOOP
-- the most brutal way to kill a session
EXECUTE IMMEDIATE 'alter system disconnect session ''' || session.sid || ',' || session.serial# || ''' immediate';
END LOOP;
-- killing is done in the background, so we need to wait a bit
LOOP
SELECT COUNT(*)
INTO open_count
FROM v$session WHERE username = '@USERNAME';
EXIT WHEN open_count = 0;
dbms_lock.sleep(0.5);
END LOOP;
-- finally, it is safe to issue the drop statement
EXECUTE IMMEDIATE 'drop user @USERNAME cascade';
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment