How to remove a participant from a room?
# ask-ai
m
How to remove a participant from a room?
t
Thanks for reaching out, Muaz. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you need to remove a participant from a room in LiveKit, you can easily do this using the
removeParticipant
method available in the LiveKit server SDKs. This will disconnect the participant right away, but just a heads up—it doesn’t revoke their access token, so they could come back if given a new token. Here are some examples for different SDKs to help you out: • *Node.js SDK:*`const roomService = new RoomServiceClient(LIVEKIT_URL, API_KEY, API_SECRET); await roomService.removeParticipant(roomName, identity);` • *Python SDK:*`await room_service.remove_participant( remove=RoomParticipantIdentity( room=room_name, identity=identity, ) )` • *Kotlin SDK:*`roomServiceClient.removeParticipant(roomName, identity)` • *Go SDK:*`_, err := roomClient.RemoveParticipant(context.Background(), &livekit.RoomParticipantIdentity{ Room: roomName, Identity: identity, })` You can also do this quickly using the LiveKit CLI:
Copy code
lk room participants remove --room=ROOM_NAME --identity=IDENTITY
To make sure the participant can’t rejoin, just keep their access token’s TTL short or avoid giving them a new token altogether. Sources: Managing participants | LiveKit Docs | packages/livekit-server-sdk/src/RoomServiceClient.ts | livekit-api/livekit/api/room_service.py | src/main/kotlin/io/livekit/server/RoomServiceClient.kt | cmd/lk/room.go
m
I need an example using python sdk, which takes in participant identity and room_name to remove a participant from a room, and can we provide reason for removal?
t
Here’s a friendly example using the Python SDK to remove a participant from a room:
Copy code
from livekit_server_sdk import RoomServiceClient, RoomParticipantIdentity

# Initialize the RoomServiceClient
room_service = RoomServiceClient(LIVEKIT_URL, API_KEY, API_SECRET)

# Remove the participant
await room_service.remove_participant(
    remove=RoomParticipantIdentity(
        room=room_name,
        identity=participant_identity,
    )
)
Just a heads-up: the Python SDK doesn’t let you add a custom reason for removal directly. But no worries! You can send a custom message to the participant before removing them by using the
send_data
method. Here’s how you could do it:
Copy code
from livekit_server_sdk import SendDataRequest, DataPacket

# Send a custom message to the participant
await room_service.send_data(
    SendDataRequest(
        room=room_name,
        data=b"You have been removed for violating community guidelines.",
        kind=DataPacket.Kind.RELIABLE,
        destination_identities=[participant_identity],
        topic="removal_reason"
    )
)

# Then remove the participant
await room_service.remove_participant(
    remove=RoomParticipantIdentity(
        room=room_name,
        identity=participant_identity,
    )
)
This way, the participant gets a clear, custom message before they’re removed. If you want to chat more about this or need any other help, just let me know—I’m here for you! Sources: livekit-protocol/livekit/protocol/room.pyi | Connecting to LiveKit | LiveKit Docs | agent/game_host.py | Managing participants | LiveKit Docs