abundant-alligator-24067
03/25/2026, 5:42 PMtall-airline-16756
03/25/2026, 5:42 PMtall-airline-16756
03/25/2026, 5:42 PMasync def my_shutdown_hook():
# cleanup tasks, save state, etc.
...
ctx.add_shutdown_callback(my_shutdown_hook)
1. Shut down the session/context (graceful):
# Non-blocking graceful shutdown that drains pending work
ctx.shutdown(drain=True)
# Or await close to block until shutdown completes
await ctx.aclose()
When you call ctx.shutdown() or await ctx.aclose(), registered shutdown callbacks (added via add_shutdown_callback) will run.
1. Delete the room (disconnects all participants) using the server API if you need the room removed immediately:
api_client = api.LiveKitAPI(
os.getenv("LIVEKIT_URL"),
os.getenv("LIVEKIT_API_KEY"),
os.getenv("LIVEKIT_API_SECRET"),
)
await api_client.room.delete_room(api.DeleteRoomRequest(room=room_name))
Node.js / TypeScript
1. Register a shutdown callback:
ctx.addShutdownCallback(() => {
// cleanup tasks
});
1. Shut down the session/context:
// Non-blocking graceful shutdown that drains pending work
ctx.shutdown(drain=true);
// Or await close to block until shutdown completes
await ctx.aclose();
Registered callbacks added with addShutdownCallback are invoked after shutdown.
1. Delete the room via the RoomService API:
const roomServiceClient = new RoomServiceClient(
process.env.LIVEKIT_URL,
process.env.LIVEKIT_API_KEY,
process.env.LIVEKIT_API_SECRET,
);
await roomServiceClient.deleteRoom(roomName);
Recommended order
1. Register needed shutdown callbacks early (so they will run on shutdown).
2. Call ctx.shutdown(drain=true) or await ctx.aclose() to trigger the callbacks and gracefully stop the session.
3. If you also need the room removed from the server immediately, call the server API deleteRoom (Node) / room.delete_room (Python) — this disconnects any remaining participants.
If you want, tell me:
• Which language (Python or Node.js) and runtime you’re using,
• Whether you need the delete-room call to run before, during, or after shutdown,
and I’ll give a tailored snippet showing the exact sequence.
Sources: Job lifecycle | LiveKit Documentation, with interactive elementsabundant-alligator-24067
03/25/2026, 5:43 PMtall-airline-16756
03/25/2026, 5:43 PMctx.add_shutdown_callback(...).
• Call ctx.shutdown(drain=True) (non-blocking) or await ctx.aclose() (awaits completion) to trigger callbacks.
• Use the LiveKit server API room.delete_room(...) to remove the room and disconnect participants.
Example (async) — complete flow
1. Register callback, 2. trigger shutdown and wait, 3. delete the room.
import os
import asyncio
from livekit import api # replace with actual LiveKit API client import
# assume ctx is your session/context object and room_name is known
async def my_shutdown_hook():
print("Running shutdown hook: cleanup tasks here")
# e.g., save state, close resources, notify other systems
# Register the callback
ctx.add_shutdown_callback(my_shutdown_hook)
# Trigger graceful shutdown and wait for callbacks to finish
# Option A: non-blocking shutdown (drain pending work)
ctx.shutdown(drain=True)
# Option B: await close to block until shutdown completes (recommended if you want to ensure hooks finished)
await ctx.aclose()
# Optionally delete the room from the server (disconnects all participants)
api_client = api.LiveKitAPI(
os.getenv("LIVEKIT_URL"),
os.getenv("LIVEKIT_API_KEY"),
os.getenv("LIVEKIT_API_SECRET"),
)
await api_client.room.delete_room(api.DeleteRoomRequest(room=room_name))
Notes and recommendations
• If you must ensure your hooks complete before proceeding, use await ctx.aclose() rather than a non-blocking ctx.shutdown(drain=True).
• Call delete_room after shutdown if you want the server-side room removed once your session has finished cleanup. If you need the room deleted immediately (while your process is still running), you can call delete_room earlier — it will disconnect participants.
• If you want, I can adjust this example to match your exact context object name, or show a pattern that runs delete-room only if shutdown completes successfully. Which would you prefer?, with interactive elementsabundant-alligator-24067
03/25/2026, 5:46 PMtall-airline-16756
03/25/2026, 5:47 PM