Updated:

Turn it on

VersionCommand
Java 1.21.11 and later/gamerule minecraft:keep_inventory true
Java 1.21.10 and earlier/gamerule keepInventory true
Bedrock Edition/gamerule keepinventory true

Java 1.21.11 renamed every game rule, so keepInventory from older videos gives "unknown game rule" on current versions. The minecraft: prefix is optional. Use false to turn it off again.

Check whether it is on

/gamerule minecraft:keep_inventory

Without a value, the command prints the current setting.

What is kept

Turn it on without typing (Bedrock)

Bedrock has a switch for it: open the world settings, go to the Cheats section and turn on Keep Inventory. On a Realm the owner does this from the Realm's world settings. Turning cheats on disables achievements for that world.

Who it affects

The rule applies to every player in the world at once, including hardcore worlds. There is no built-in way to keep items for only some players; servers that want that use a plugin or data pack.

Items with Curse of Vanishing

Curse of Vanishing only removes items when they would drop on death. With keep inventory on, nothing drops, so cursed items stay in the inventory too.

Getting your items back when it is off

With keep inventory off, everything drops where you died. Dropped items disappear after five minutes while that area is loaded, so go back quickly.

/data get entity @s LastDeathLocation

In Java 1.19 and later this shows where you last died. A recovery compass points to the same spot. On Bedrock, turn on Show Coordinates so you can note your position before risky trips.

Common problems

For other renamed rules, see the gamerule list.

Check your command

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