Add configurable cleanup on middleware