Updated:

Pick by goal

GoalUse
No hostile mobs at all/difficulty peaceful
No new natural spawns, animals includedThe mob spawning game rule
No phantoms onlyThe phantom game rule
Only around your baseLight, or /fillbiome (Java)

Peaceful

/difficulty peaceful

Hostile mobs stop spawning and the ones already there disappear. Health regenerates on its own too, so the game plays differently. See the difficulty guide.

Game rules

StopsJava 1.21.11+Java 1.21.10 and earlierBedrock
Natural mob spawning (animals too)minecraft:spawn_mobsdoMobSpawningdomobspawning
Phantomsminecraft:spawn_phantomsdoInsomniadoinsomnia
/gamerule minecraft:spawn_mobs false

That is the 1.21.11+ form. Turning off mob spawning also stops new animals. Spawners keep working, so mob farms built on spawners still run. Mobs already in the world stay, so clear them as shown below.

Phantoms appear at night when you have not slept for a while. Turning off only the phantom rule leaves every other mob as normal.

Clear the mobs already there

/kill @e[type=minecraft:zombie]
/kill @e[type=minecraft:creeper,distance=..50]

Name the mob type. A bare /kill @e also removes players, pets and armor stands. The easiest way is to switch to Peaceful for a moment and then back. See the kill guide.

Only around your base

When it does not work

Common questions

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.