Kill and Disconnect Oracle Sessions with ALTER SYSTEM
Kill and Disconnect Oracle Sessions with ALTER SYSTEM
Purpose
V$SESSION exposes the SID and SERIAL# pair that both ALTER SYSTEM KILL SESSION and ALTER SYSTEM DISCONNECT SESSION require as their targeting key. Pull the SID and SERIAL# pair first. A wrong pair can end the wrong connection, because Oracle reuses SIDs. Once a session is confirmed as the target — a runaway report query burning CPU, an abandoned connection holding a row lock, an idle session pinning undo — use one of the commands below.
ALTER SYSTEM has two termination commands. They behave differently. KILL SESSION marks the session for termination and, when qualified with IMMEDIATE, forces the rollback of any uncommitted work right away. Cleanup then releases the session's resources. DISCONNECT SESSION targets the underlying server process rather than the session state and is the correct tool when the client connection is gone but the session's server process is still attached. Use it after a client crash or network drop. DISCONNECT SESSION accepts a POST_TRANSACTION qualifier as an alternative to IMMEDIATE: it lets any current transaction finish and commit or roll back on its own timeline before the disconnect happens, rather than forcing an immediate rollback.
Run a blocking-session query first to find the SID, then use these commands to end it.
Code
1-- Step 1: identify the session, sourcing SID, SERIAL#, and status from V$SESSION
2SELECT s.sid,
3 s.serial#,
4 s.username,
5 s.status,
6 s.blocking_session,
7 s.program,
8 s.logon_time,
9 s.last_call_et
10FROM v$session s
11WHERE s.username = 'APPUSER' -- filter by username, SID, or blocking_session
12 AND s.status IN ('ACTIVE', 'INACTIVE')
13ORDER BY s.last_call_et DESC;
14
15-- Step 2a: kill the session, rolling back any transaction immediately
16ALTER SYSTEM KILL SESSION '142,8821' IMMEDIATE;
17
18-- Step 2b: disconnect the server process, letting the current transaction finish first
19ALTER SYSTEM DISCONNECT SESSION '142,8821' POST_TRANSACTION;
20
21-- Step 2c: disconnect the server process right away, no wait for the transaction
22ALTER SYSTEM DISCONNECT SESSION '142,8821' IMMEDIATE;
23
24-- Step 3: confirm the session status after issuing the kill
25SELECT s.sid,
26 s.serial#,
27 s.status,
28 s.username
29FROM v$session s
30WHERE s.sid = 142 AND s.serial# = 8821;
31-- STATUS = 'KILLED' means the session is marked; PMON has not finished cleanup yet.
32-- No row returned means the session (this SID,SERIAL# pair) is gone.
33
34-- RAC: target a session on a specific instance by appending the instance ID
35ALTER SYSTEM KILL SESSION '142,8821,@2' IMMEDIATE;
Code Breakdown
Step 1: sourcing SID and SERIAL# from V$SESSION
V$SESSION.SID and V$SESSION.SERIAL# together form the exact key both ALTER SYSTEM commands need. SID alone is not a safe identifier — Oracle reuses SID values as sessions log off and new ones log on, so a SID that pointed at a stuck report query five minutes ago can point at an unrelated new connection now. SERIAL# increments with every reuse of a SID, so the pair together always identifies one specific, current session. STATUS, BLOCKING_SESSION, PROGRAM, and LAST_CALL_ET (elapsed seconds since the session's last call) give the context needed to confirm this is the right session before issuing a termination command against it.
Step 2a: KILL SESSION IMMEDIATE
ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE is the standard command for ending a session whose Oracle-side process is still alive and responsive. The IMMEDIATE qualifier tells Oracle to roll back any uncommitted transaction right away rather than waiting. Oracle marks the session for termination; background cleanup — coordinated by PMON and carried out in Oracle Database 19c by the CLMN cleanup process and its CLnn workers — releases locks, frees the process, and removes the session's entry from V$SESSION. On a busy instance, or with a very large rollback, cleanup takes longer.
Step 2b and 2c: DISCONNECT SESSION with POST_TRANSACTION or IMMEDIATE
ALTER SYSTEM DISCONNECT SESSION targets the dedicated server process associated with the session rather than the session's logical state. It is the right tool when the client connection is gone at the network or OS level but the server-side process Oracle allocated for that client is still holding resources — the classic symptom after a client machine loses power or a VPN drops without a graceful disconnect. POST_TRANSACTION lets any transaction the session was mid-way through commit or roll back through its normal completion path before the server process is released, avoiding an unnecessary forced rollback of work that was about to finish anyway. IMMEDIATE skips that wait and forces the disconnect — and any needed rollback — right away, Use it when the client is confirmed gone and the transaction will not finish on its own.
Step 3: confirming termination
Re-querying V$SESSION for the same SID and SERIAL# after issuing a kill shows one of two states. A STATUS of KILLED means Oracle has marked the session for termination but PMON has not yet finished the actual cleanup — the row is still present but the session is not accepting new work. No row returned at all means cleanup is complete and the session no longer exists in V$SESSION. Filtering on SID alone is not enough here: after cleanup, a new and unrelated session can take the same SID, and its row would look like a failed kill.
RAC: targeting a specific instance
On a Real Application Clusters configuration, a session belongs to one specific instance. Appending @<instance_id> to the SID,SERIAL# string in the ALTER SYSTEM command — for example '142,8821,@2' — targets the session on instance 2 directly from any instance in the cluster, without needing to connect to that specific instance first.
Key Points
KILL SESSIONandDISCONNECT SESSIONtarget different things.KILL SESSIONacts on the session's logical state inside Oracle;DISCONNECT SESSIONacts on the underlying server process. A session whose client vanished at the network level, leaving an orphaned server process, is aDISCONNECT SESSIONcase even thoughKILL SESSIONmay also clear it.SIDalone is never a safe kill target. Always pair it withSERIAL#from the sameV$SESSIONquery used to identify the session, run immediately before the kill, to avoid aSIDreuse race.STATUS = 'KILLED'is a marked-for-kill state, not a completed termination.PMONand its cleanup processes release resources asynchronously; aKILLEDrow can persist, and withoutIMMEDIATEOracle does not recover the full session state until the killed session's client makes another call.ORA-00031: session marked for killmeans the kill is still in progress. If the session is doing work that must complete first, such as rolling back a transaction, and the wait lasts a minute, Oracle marks the session to be terminated and returns control with this message;PMONthen marks the session as terminated when the activity is complete.- RAC sessions require the
@instance_idsuffix when killed from a different instance. Oracle requires the issuing session and the target session to be on the same instance unless the instance ID is given; without it, the kill does not reach the session on the other node. - A very large uncommitted transaction extends cleanup time regardless of
IMMEDIATE.IMMEDIATEstarts the rollback immediately, but the rollback itself still has to undo every change the transaction made — a multi-hour batch load killed near its end can take a comparable amount of time to roll back.
Insights and Best Practices
When IMMEDIATE beats POST_TRANSACTION
Use IMMEDIATE as the default for KILL SESSION. Once you decide to end a session, there is little reason to let its uncommitted work continue. For DISCONNECT SESSION, the choice is less automatic. POST_TRANSACTION is worth using when the session is known to be mid-transaction and the transaction is expected to finish quickly on its own — letting it complete avoids a rollback that costs more time than simply waiting. IMMEDIATE is the right call for DISCONNECT SESSION when the client is confirmed gone (a dropped VPN, a crashed application server) and there is no expectation the transaction will ever resume progress toward a natural commit.
Reading the ORA-00031 wait
ORA-00031 means the kill request is still being processed. Wait and re-check V$SESSION. Querying STATUS for the same SID,SERIAL# on a short interval — a few seconds apart — shows whether the row eventually disappears (cleanup completed) or persists indefinitely (cleanup is stuck, which usually points at a large rollback still in progress, PMON itself under load, or the killed client has not yet made another call). No further action is required for the session to be killed, but reissuing ALTER SYSTEM KILL SESSION for the same SID,SERIAL# may end the session sooner.
RAC considerations
On RAC, confirm which instance owns the target session before issuing the kill. GV$SESSION.INST_ID in the cluster-wide view GV$SESSION shows the owning instance directly, which avoids guessing. A kill issued without the @instance_id suffix against a session that belongs to a different instance than the one you are connected to will not terminate the intended session.
Verifying termination completed
Treat the disappearance of the row from V$SESSION — not the successful execution of the ALTER SYSTEM statement — as the actual confirmation that termination completed. The ALTER SYSTEM command can wait before it returns. When the session must finish work first, such as a rollback or a reply from a remote database, Oracle waits for it and marks the session terminated before returning control. For a session holding a lock that another process is waiting on, re-check the blocking session's own downstream effect — has the blocked session's wait cleared — as the final confirmation that the problem is solved.
When to Run This
- A runaway ad-hoc query is consuming CPU or I/O far beyond its expected footprint and needs to stop immediately.
- A session is confirmed as the blocker in a lock-contention diagnostic and holding up other work.
- A client machine or network path is known to have dropped, leaving an orphaned server-side process still attached to the instance.
- An idle session has held undo or a long-running transaction open well past any reasonable business need, pinning resources other sessions require.
- A maintenance window requires clearing all non-essential sessions before a planned shutdown or upgrade step.
- A session appears twice in monitoring because a client reconnected without the previous connection ever closing cleanly.
Troubleshooting Common Issues
ORA-00031: session marked for kill after issuing a kill. The session was marked to be terminated, but the work it must finish first (such as a rollback) has not completed yet. Re-query V$SESSION for the same SID,SERIAL# after a short wait, or reissue the same kill, which may end the session sooner; the row will clear once cleanup completes.
The session row disappears from V$SESSION but the resource it was holding — a lock, a piece of undo — does not appear to be released. Confirm the check is being run against a fresh query, not a cached result set, and check whether a downstream blocked session's wait has actually cleared rather than only checking the killed session's own row.
ALTER SYSTEM DISCONNECT SESSION returns successfully but the session reappears shortly after. This usually means the client reconnected using a connection pool or auto-reconnect logic rather than the original connection surviving. Confirm whether the reappearing session is a genuinely new connection (a fresh SID,SERIAL# pair and LOGON_TIME) before assuming the disconnect failed.
A kill against a RAC session appears to have no effect. Confirm the target session's owning instance with GV$SESSION.INST_ID and reissue the command with the correct @instance_id suffix if the session lives on a different node than the one the kill was issued from.
References
- ALTER SYSTEM — SQL Language Reference 19c - Canonical syntax reference for KILL SESSION and DISCONNECT SESSION, including the IMMEDIATE and POST_TRANSACTION qualifiers
- V$SESSION — Oracle Database Reference 19c - Column reference for V$SESSION, including SID, SERIAL#, STATUS, and BLOCKING_SESSION used to source the kill target
- oracle-base.com - Tim Hall's Oracle reference site
- asktom.oracle.com - Tom Kyte's Q&A archive on kill-session and ORA-00031 questions
Posts in this series
- Monitoring Active and Idle User Sessions in Oracle Database
- Identifying Idle User Sessions in Oracle Database
- Tracking Idle Time for User Sessions in Oracle Database
- Oracle Map Sessions to OS Processes via v$session
- Monitoring User-Specific SQL Activity in Oracle Database
- v$session v$process: Map Oracle Sessions to OS PIDs
- Monitoring Opened Cursors by User Session in Oracle Database
- Oracle Active SQL: Monitor Sessions with v$session
- Tracking Completed Long Operations in Oracle Database
- Check RESUMABLE_TIMEOUT and RESUMABLE Status in V$SESSION
- Kill and Disconnect Oracle Sessions with ALTER SYSTEM