diff --git a/pi_mqtt_gpio/modules/__init__.py b/pi_mqtt_gpio/modules/__init__.py
index 6f2892ffd8221f395d8dcde27c0f4beed6e78e20..2e828939c981ba761a7461751164d73b60edda9f 100644
--- a/pi_mqtt_gpio/modules/__init__.py
+++ b/pi_mqtt_gpio/modules/__init__.py
@@ -44,3 +44,9 @@ class GenericGPIO(object):
     @abc.abstractmethod
     def get_pin(self, pin):
         pass
+
+    def cleanup(self):
+        """
+        Called when closing the program to handle any cleanup operations.
+        """
+        pass
diff --git a/pi_mqtt_gpio/modules/raspberrypi.py b/pi_mqtt_gpio/modules/raspberrypi.py
index a1b4174df0218c6b2b71aae62d247e86c0708c37..8f505c936d3ef8b47e5c1a9ec95ddeef8b1254f0 100644
--- a/pi_mqtt_gpio/modules/raspberrypi.py
+++ b/pi_mqtt_gpio/modules/raspberrypi.py
@@ -43,3 +43,6 @@ class GPIO(GenericGPIO):
 
     def get_pin(self, pin):
         return self.io.input(pin)
+
+    def cleanup(self):
+        self.io.cleanup()
diff --git a/pi_mqtt_gpio/server.py b/pi_mqtt_gpio/server.py
index f16c68ac5e6a53e240e44b4ccd1fd20f68533411..1a2ab6c5451354f3b1231f90c30098c00fb77db5 100644
--- a/pi_mqtt_gpio/server.py
+++ b/pi_mqtt_gpio/server.py
@@ -488,3 +488,9 @@ if __name__ == "__main__":
     finally:
         client.disconnect()
         client.loop_stop()
+        for name, gpio in GPIO_MODULES.items():
+            try:
+                gpio.cleanup()
+            except Exception:
+                _LOG.exception(
+                    "Unable to execute cleanup routine for module %r:", name)