how to remove or make the agent leave the room.
# ask-ai
p
how to remove or make the agent leave the room.
t
Thanks for reaching out, Bryner. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you with removing or making an agent leave a room in LiveKit! You can easily do this using the removeParticipant method provided by the RoomServiceClient. This will gently disconnect the participant from the room and trigger a
Disconnected
event for them. Here's a friendly example in Node.js:
Copy code
const roomService = new RoomServiceClient(LIVEKIT_URL, API_KEY, API_SECRET);
await roomService.removeParticipant('roomName', 'agentIdentity');
And here’s how you can do it in Python:
Copy code
from livekit import api

lk_api = api.LiveKitAPI("wss://your-livekit-url", "API_KEY", "API_SECRET")
await lk_api.room.remove_participant(
    room="roomName",
    identity="agentIdentity"
)
If you prefer the agent to disconnect itself in a smooth and graceful way, you can call
session.shutdown()
within the agent’s logic. This helps make sure any necessary cleanup happens before the agent leaves the room. Please feel free to reach out if you have any more questions or need a hand with anything else—I’m here to support you! Sources: pkg/rtc/room.go | Job lifecycle | LiveKit Docs | Managing participants | LiveKit Docs | Managing rooms | LiveKit Docs
p
how to delete room gracefully
t
I'd be happy to help you delete a room gracefully in LiveKit! You can use the DeleteRoom API for this. When you do, it will disconnect all participants and clear the room's state, including routing info and any stored data. Here's a friendly example in Python:
Copy code
from livekit.api import LiveKitAPI
import asyncio

async def delete_room():
    lkapi = LiveKitAPI()
    await lkapi.room.delete_room(DeleteRoomRequest(room="your-room-name"))
Also, if you're working with LiveKit Agents, a handy tip is to set
delete_room_on_close=True
in
RoomInputOptions
. This way, the room will be deleted automatically when the agent session ends. If you have any questions or want me to walk you through this step-by-step, just let me know—I’m here to help! Sources: pkg/service/roomservice.go | Connecting to LiveKit | LiveKit Docs | Managing rooms | LiveKit Docs | examples/voice_agents/error_callback.py | Avoiding Room ID Reuse When Re-Creating Rooms With the Same Name