# Clearing MQTT retained messages

**URL:** <https://community.openhab.org/t/clearing-mqtt-retained-messages/58221>\
**Category:** Tutorials & Examples\
**Tags:** mosquitto, mqtt\
**Created:** [November 29, 2018, 10:05am UTC](https://community.openhab.org/t/clearing-mqtt-retained-messages/58221 "2018-11-29T10:05:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [November 29, 2018, 10:05am UTC](https://community.openhab.org/t/clearing-mqtt-retained-messages/58221/1 "2018-11-29T10:05:03Z")

</div>

# Clearing retained messages in mosquitto

## Purpose
The purpose of this tutorial is to present solutions to clear retained messages in the [Eclipse Mosquitto MQTT Broker](https://mosquitto.org/).

Retained messages can be troublesome and/or unwanted. Many mosquitto tutorials included a retain option in the configuration and setup of mosquitto. This is likely to create problems in the future.

This tutorial is created for Linux users with a package installation on mosquitto.  
Other users will have to find the files and paths relevant to their system.

## Clearing ALL retained messages

1. First we have to stop the mosquitto broker.

2. Delete the `mosquitto.db` containing all the stored message data in the persistence. By default, located in `/var/lib/mosquitto/mosquitto.db`

3. Restart the mosquitto service

## Clearing SPECIFIC retained messages

In some cases some retained messages can be wanted and we don’t want to clear all the retained messages.

The messages will have to be cleared one by one using the **topic**

To clear a specific message:

> mosquitto\_pub -h **_hostname_** -t **_the/topic_** -n -r -d

  
If your broker is setup with a username and password then use:

> mosquitto\_pub -h **_hostname_** -t **_the/topic_** -u **_username_** -P **_password_** -n -r -d

- Replace the **_hostname_** with the hostname or IP address of your broker (e.g. `localhost`)
- Replace **_the/topic_** with the topic that you want the retained messages cleared for
- Replace the **_username_** and **_password_** with… Well you know…

Options used in the [mosquitto\_pub](https://mosquitto.org/man/mosquitto_pub-1.html) command:

> `-n` = Send a null (zero length) message  
> `-r` = Retain the message as a “last known good” value on the broker  
> `-d` = Enable debug messages

I hope this is useful.  
I have made this thread a wiki so please don’t hesitate to make changes if you see a glaring error or you want to add other operating system instructions.

---

<div class="post-metadata">

**Author:** ![Dim](https://community.openhab.org/user_avatar/community.openhab.org/dim/32/4568_2.png) [@Dim](https://community.openhab.org/u/Dim)\
**Post date:** [November 29, 2018, 10:27am UTC](https://community.openhab.org/t/clearing-mqtt-retained-messages/58221/2 "2018-11-29T10:27:34Z")

</div>

cool !

I used to use another method to clear the retained messages also:

```auto
mosquitto_pub -h hostname -t the/topic -m

```

(`-m` = message with no content)

I never checked if this worked (since I didn’t set the retain option on the blank message 😛)

---

<div class="post-metadata">

**Author:** ![WJ4IoT](https://community.openhab.org/user_avatar/community.openhab.org/wj4iot/32/95810_2.png) [@WJ4IoT](https://community.openhab.org/u/WJ4IoT)\
**Post date:** [March 13, 2019, 9:28am UTC](https://community.openhab.org/t/clearing-mqtt-retained-messages/58221/3 "2019-03-13T09:28:09Z")

</div>

F.Y.I Just sharing: Method I use is with the Windows 10 app “MQTTbox”.

- inside the APP connect to your MQTT server when connected
- add publisher with your topic to be cleared, retain=yes and empty payload (payload type “strings…”)
- publish

MQTTbox also provides a simple method to monitor your MQTT traffic, so far I like MQTTbox over other APPs to do this simple tasks without finding out the correct “mosquitto\_pub” string.

---

<div class="post-metadata">

**Author:** ![xAPPO](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/bc79bd/32.png) [@xAPPO](https://community.openhab.org/u/xAPPO)\
**Post date:** [March 15, 2019, 12:54am UTC](https://community.openhab.org/t/clearing-mqtt-retained-messages/58221/4 "2019-03-15T00:54:25Z")

</div>

MQTT Explorer has the ability to recursively delete retained topics. 50 at a time (click)

> **[GitHub - thomasnordquist/MQTT-Explorer: An all-round MQTT client that provides a...](https://github.com/thomasnordquist/MQTT-Explorer)**
>
> An all-round MQTT client that provides a structured topic overview

There’s also MQTT Forget  
[https://www.npmjs.com/package/mqtt-forget](https://www.npmjs.com/package/mqtt-forget)

---

<div class="post-metadata">

**Author:** ![kniight](https://community.openhab.org/letter_avatar_proxy/v4/letter/k/977dab/32.png) [@kniight](https://community.openhab.org/u/kniight)\
**Post date:** [April 10, 2021, 9:52pm UTC](https://community.openhab.org/t/clearing-mqtt-retained-messages/58221/5 "2021-04-10T21:52:05Z")

</div>

Simple shell script to delete all entries under “homie/examplehomiedev” :

```auto
mosquitto_sub -h localhost -W 1 -F '%t' -t '#' | grep '^homie/examplehomiedev' | while read i ; do mosquitto_pub -h localhost -r -d -t "$i" -n; done

```

It should be one line in the shell.

---

<div class="post-metadata">

**Author:** ![m4rk](https://community.openhab.org/user_avatar/community.openhab.org/m4rk/32/49247_2.png) [@m4rk](https://community.openhab.org/u/m4rk)\
**Post date:** [July 22, 2025, 9:11am UTC](https://community.openhab.org/t/clearing-mqtt-retained-messages/58221/6 "2025-07-22T09:11:07Z")

</div>

For testing I use MQTT snooper, free app, on my phone to subscribe to topics. There is an option to delete publications from the server, which I successfully used to delete old messages which kept appearing when initially subscribing.
