Minecraft kill all players except a certain player

I'm making a house, with a security system, where only I am allowed. My command block is supposed to kill all players except me, but I can't find what command to use. I've tried /kill @a[name=!<My username], but it doesn't work. What is the actual command


You can use the scoreboard teams feature.

First, create a team: /scoreboard teams add <name>
Then, add the player you don't want to kill to the team: /scoreboard teams join <team> <player>
Finally, kill the everyone apart from the team: /kill @a[team=!<team name>]

So if you wanted to kill everyone except for team Arqade which had you in it, you would do:
/scoreboard teams add Arqade
/scoreboard teams join Arqade <Your Username>
/kill @a[team=!Arqade]
Replacing '<Your Username>' with your in-game Minecraft username.

If you want to remove someone from a team, you do /scoreboard teams leave <player>


EmeraldEye's answer works, but it removes all information on what team someone was if teams were used already. You could instead use tags, but there is an even easier way, in one command:

/execute at <player_name> as @a unless entity @s[distance=0] run kill @s

This is easy to write, but hard to understand. What this does is first shifting the execution position to the player you want to not kill, but still executing as whatever executes the command (a command block for example). Then it changes the execution context to be done by all players, without changing the position where it executes. It also splits up all further parts of the command, so that they run once per player. Then it checks if the selected player has distance 0 from the execution position (which is still the one player's position that you don't want to kill) and if they do not have distance 0, they get killed.
The advantage of checking for distance 0 and negating the selection is that it also selects people in other dimensions, unlike something like if entity @s[distance=0.001..] would do.

Keep in mind that this command will not kill players that are on the exact same position as the player you don't want to kill. But that can only happen in rare situations like respawning, dismounting a minecart at a fixed position, teleporting, … and then not moving before the other player does the same.