Skip to content

Instantly share code, notes, and snippets.

@reubenmiller
Last active April 30, 2026 14:06
Show Gist options
  • Select an option

  • Save reubenmiller/abefa3d6f17f14cff381a1a8ea28238b to your computer and use it in GitHub Desktop.

Select an option

Save reubenmiller/abefa3d6f17f14cff381a1a8ea28238b to your computer and use it in GitHub Desktop.
script to rest thin-edge.io state (but persist the device certificates)

tedge-reset

What does it do?

The script does the following things (but please read the script yourself before executing it):

  • stop the main tedge services
  • stop mosquitto
  • delete the mosquitto database
  • delete internal tedge state
  • delete thin-edge.io file based logs and file-transfer cache

Afterwards, it will prompt the user if they wish to reconnect. It also directs the user to delete the device (and all child managed objects) from the cloud before reconnecting.

How to use it

Copy the tedge-reset.sh script to the device, and then run the following commands:

sudo chmod a+x ./tedge-reset.sh
sudo ./tedge-reset.sh

Deleting the device from the cloud

When deleting the device from the cloud, if you're using go-c8y-cli, then you MUST use the --cascade option to ensure that all of the child devices are also deleted, otherwise it can result in unlinked services and child devices.

Below shows an example of the correct usage to delete the device and all of its child devices and services.

c8y devices list --name "example01" | c8y devices delete --cascade

In the event that you have deleted the device managed object and not the child devices and services, then you will have to manually delete them using the following command where all inventory managed object which are owned by the device's user are searched for then deleted (note: that the owner has the device_ prefix):

WARNING: Please verify the find results before deleting it to ensure the query is returning the expected amount of inventory managed objects!

c8y inventory find --owner "device_example01" --includeAll | c8y inventory delete
#!/bin/sh
set -e
# allow for custom installation directories
TEDGE_CONFIG_DIR="${TEDGE_CONFIG_DIR:-/etc/tedge}"
stop_services() {
tedge disconnect c8y
systemctl stop tedge-agent
systemctl stop mosquitto
systemctl stop tedge-container-plugin >/dev/null 2>&1 ||:
}
delete_state() {
# NOTE: This assumes the default mosquitto.db location, but
# users can change this location, so it is up to the users to
# remove the mosquitto.db themselves
if [ -f /var/lib/mosquitto/mosquitto.db ]; then
rm -f /var/lib/mosquitto/mosquitto.db
echo "Deleted the /var/lib/mosquitto/mosquitto.db file"
fi
echo "Removing internal thin-edge.io state" >&2
rm -rf "$TEDGE_CONFIG_DIR/.agent/"
rm -rf "$TEDGE_CONFIG_DIR/.tedge-mapper-c8y/"
if [ -n "$(find "$TEDGE_CONFIG_DIR/operations/c8y/" -type d -mindepth 1 -maxdepth 1 2>/dev/null ||:)" ]; then
find "$TEDGE_CONFIG_DIR/operations/c8y/" -type d -mindepth 1 -maxdepth 1 -exec rm -rf {} \;
fi
echo "deleting logs" >&2
LOGS_PATH=$(tedge config get logs.path)
if [ -d "$LOGS_PATH" ]; then
rm -f "$LOGS_PATH"/agent/*.log
fi
DATA_PATH=$(tedge config get data.path ||:)
if [ -d "$DATA_PATH" ]; then
rm -rf "$DATA_PATH"
fi
# optional: remove the device certificate, but if this were to be done
# then the device would also have to be re-registered with Cumulocity
# which is more involved
#rm -f /etc/tedge/device-certs/*
}
post_start() {
tedge connect c8y
systemctl start tedge-container-plugin >/dev/null 2>&1 ||:
}
main() {
stop_services
delete_state
# Prompt user if it is ok to start services again
DEVICE_ID=$(tedge config get device.id ||:)
printf "Now delete the item from the cloud (c8y_Serial=%s) and then type 'y': " "$DEVICE_ID" >&2
read -r ANSWER
case "$ANSWER" in
y|Y|yes)
echo "Connecting thin-edge.io again to Cumulocity" >&2
post_start
;;
*)
echo "NOTE: thin-edge.io was not reconnected, please run 'tedge reconnect c8y' when you're ready" >&2
;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment