The bug
When using /setblock or /fill to replace a block entity, this keeps the old NBT (if no NBT is specified), but clears the inventories, even if the command fails.
Additionally, the command will fail if the specified block state matches the existing one, even if there is new NBT to apply. The result is only inventories clearing.
How to reproduce
Example 1:
/setblock ~1 ~ ~ dropper[facing=up]{CustomName:"\"first\"",Items:[{id:stone,Count:1b,Slot:0b}]}
This places a dropper as expected. Now run the following command:
/setblock ~1 ~ ~ dropper[facing=up]{CustomName:"\"updated\"",Items:[{id:stone,Count:1b,Slot:0b}]}
Expected behavior: command succeeds (because NBT differs), dropper updates name.
Actual behavior: command fails (because the blockstates are the same), dropper does not update name, inventory clear.
Note: Leaving off the NBT in the last command has the same effect.
Example 2:
/setblock ~1 ~ ~ command_block[facing=up]{Command:"first"}
This places a command block as expected. Now run the following command:
/setblock ~1 ~ ~ command_block[facing=down]
Expected behavior: command block is empty
Actual behavior: command block contains the command "first".
Note: including NBT in the last command successfully places a new block as expected.
Related issues
is duplicated by
relates to
Comments

Hey @Bertie2011, we did some testing and I rewrote your report to be more detailed. Hope you don't mind!

@tryashtar
No, I don't mind. I didn't have much time to test things out, thanks for expanding my post =)

Could be related to MC-50166 since replacing all blocks with air / barrier blocks first was likely done to remove tile entities.

Confirmed in Minecraft 1.12 Pre Release 4

Confirmed in Minecraft 1.12 Pre Release 5

Can confirm for 1.12 Pre-7

Confirmed for 1.12!
I hoped that the delay would mean a fix before the release. 😞
EDIT:
Possible temporary fix for map makers: Use the destroy argument instead of the replace argument and make sure to do: /gamerule doTileDrops false. This prevents the block to appear as item after destruction.

The same issue with me, too!
Was not fixed with the new command system. Updated the syntax of the commands to 1.13's format.

Confirmed for snapshot 17w50a. Hope this finds its way into 1.13, with all those nbt-related command changes.

Confirmed for 1.13-pre2

Affects 1.13-pre4

Affects 1.13-pre5

Affects 1.13-pre6

Affects 1.13-pre7

And 1.13-pre8

And 1.13-pre9

And 1.13

And 18w30a

And 18w30b

And 18w31a

Confirmed for 18w46a.

Still in 19w08a

Please do not mark unreleased versions as affected.
You don't have access to them yet.

Still in 1.14.1 Release

Present in 19w44a

Still seeing this in 1.16.3
Trying to use fill command to replace end_gateway blocks with different nbt results in No blocks filled message.

It seems like this issue doesn't necessarily have anything to do with inventories. What happens is that the target block just gets "cleared" of data, and simply not (re)populated with the specified NBT.
As if replacing a block with differing NBT is interpreted as invalid data for the block and just forced erased upon replace.

I've had the same problem with lecterns, and no matter how much I replaced the block with air and then put in the new lecternwithnbt, the new lectern has the old NBTs. (although in the meantime I've put in a block of air to 'clear').
And 1.20 last pre realase.

Affects 1.21.3

Affects 24w44a
The first example is fixed in 25w02a, and the second example is now intended, per these changes in the changelog:
If the block entity data is not specified, and the existing block has data, the block entity data will be preserved
If the block entity data is specified, the block entity data will be set to the specified value
To clear the block entity data explicitly, you must now specify the block entity data as {}
The operation is now successful if either the block state changed or the block entity data changed
The bug seems to completely erase the nbt data of the block, to reprodice simply put this command in a command block and run it twice.
The first time the command is run there will be a stone block in the dropper and the second time the dropper will be empty.