A nice feature is that discovered entities can be easily removed from Home Assistant at any time. This can be done by simply publishing the original discovery topic, but using an empty payload. You can see this in the else branch in the example above:
client.publish(<original discovery topic>, "");
This will immediately delete all entities created by the original (or last) discovery message. Note that it only deletes entities, so if they have been used in automations, scripts, dashboards, etc. in Home Assistant, they should be fixed for the currently missing entities.
With the addition of a user-accessible toggle to enable/disable discovery, users can add or remove MQTT entities from their device in Home Assistant at any time simply by changing the state of this variable.