How to manually delete a room and close the sessio...
# ask-ai
a
How to manually delete a room and close the session and call the shutdown callback?
t
Thanks for reaching out, Manish. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your question about how to manually delete a room, close the session, and ensure your shutdown callback runs. You can accomplish this in three parts: (1) register a shutdown callback, (2) shut down the session/context, and (3) (optionally) delete the room via the server API. Shutdown callbacks you register will be invoked when the session/context is shut down. Below are concise examples for both Python and Node.js and the recommended order. Python 1. Register a shutdown callback:
Copy code
async def my_shutdown_hook():
    # cleanup tasks, save state, etc.
    ...

ctx.add_shutdown_callback(my_shutdown_hook)
1. Shut down the session/context (graceful):
Copy code
# 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:
Copy code
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:
Copy code
ctx.addShutdownCallback(() => {
  // cleanup tasks
});
1. Shut down the session/context:
Copy code
// 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:
Copy code
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 elements
a
I am using python
t
Thanks — I’ll provide a concise, Python-specific sequence and example showing how to (1) register a shutdown callback, (2) gracefully close the session/context so the callback runs, and (3) optionally delete the room via the server API. Important points • Register shutdown callbacks with
ctx.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.
Copy code
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 elements
a
Okay Session still showing active on livekit session tab
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!