Run cleanup at end of continuous build?

Gradle.buildFinished can be used to run something a build has finished. However, in continuous builds, this will run multiple times (each time after a new build has finished). Is there some hook that only runs once, when the continuous build is stopped/cancelled? I want to perform some cleanup of a deployment.

It seems that DeploymentHandle#stop is called at the end of a continuous build session. However, this function is not called if the gradle daemon gets killed unexpectedly (such as when pressing “stop all” when debugging from intellij).