Many animals engage in cannibalism to supplement their diets. In swarms of migratory locusts, cannibalism is prevalent and thought to be an important factor for swarm dynamics. We show that in dense swarms, locusts defend themselves by producing an anti-cannibalistic pheromone; phenylacetonitrile (PAN). Both the degree of cannibalism and the production of PAN are density dependent and co-vary. We identify the olfactory receptor responsible for the detection of PAN, and by using genome editing we show that by making this receptor non-functional we abolish the negative response to PAN. We also knock out the gene underlying PAN production and show that locusts lacking PAN lose its protection and are more frequently exposed to intra-specific predation. In conclusion, we reveal an anti-cannibalistic feature built on a specifically produced odor. The system is very likely of great importance in locust swarm dynamics and our results might therefore provide opportunities in locust management.