how to terminate the rooms using ptyhon ?
# ask-ai
a
how to terminate the rooms using ptyhon ?
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! Just to make sure I understand you correctly, are you trying to end a room using the LiveKit server API with Python? If that's the case, do you already have your LiveKit server up and running? Also, are you comfortable with how the API authentication works? I'm here to help you every step of the way!
a
yes, I am connected to cloud, I want to list all the rooms and then terminate them if any is existing already
t
Hi there! To help you list and terminate rooms using Python with the LiveKit server API, I've put together some easy steps for you to follow: 1. Listing Rooms: You can use the
list_rooms()
method to get all active rooms. Here's a simple example to guide you:
Copy code
from livekit import api
import asyncio

async def list_all_rooms():
    lkapi = api.LiveKitAPI()

    # List all rooms
    room_list = await lkapi.room.list_rooms(api.ListRoomsRequest())
    for room in room_list.rooms:
        print(f"Room: {room.name}, Participants: {len(room.participants) if room.participants else 0}")

    await lkapi.aclose()

# Run the function
asyncio.run(list_all_rooms())
2. Terminating Rooms: When you want to terminate a specific room, the
delete_room()
method is your friend. Here's how you can do it:
Copy code
from livekit import api
import asyncio

async def delete_room(room_name: str):
    lkapi = api.LiveKitAPI()

    # Delete a specific room by name
    await lkapi.room.delete_room(api.DeleteRoomRequest(room=room_name))
    print(f"Room '{room_name}' has been deleted.")

    await lkapi.aclose()

# Example usage
asyncio.run(delete_room("my-room"))
Just a quick reminder: make sure your environment variables (
LIVEKIT_API_KEY
,
LIVEKIT_API_SECRET
, and
LIVEKIT_URL
) are correctly set up, or you can pass them directly when creating your
LiveKitAPI
instance. If you have any questions or need a hand with anything else, feel free to ask—I'm here to help! Sources: examples/agent_dispatch.py | Job lifecycle | LiveKit Docs | Connecting to LiveKit | LiveKit Docs