diff --git a/cmd/export_synapse_for_import/main.go b/cmd/export_synapse_for_import/main.go
index 83397b3c11ae17f6abbd604e1197fa7026725c95..d0685bcf436823a96a6ad7c4c3459a0eb345d512 100644
--- a/cmd/export_synapse_for_import/main.go
+++ b/cmd/export_synapse_for_import/main.go
@@ -39,6 +39,8 @@ func main() {
 	config.Runtime.IsImportProcess = true
 	version.SetDefaults()
 	version.Print(true)
+
+	defer assets.Cleanup()
 	assets.SetupTemplates(*templatesPath)
 
 	var realPsqlPassword string
@@ -143,10 +145,7 @@ func main() {
 		ctx.Log.Fatal(err)
 	}
 
-	ctx.Log.Info("Done export - cleaning up...")
-
-	// Clean up
-	assets.Cleanup()
+	ctx.Log.Info("Done export")
 
 	// Report missing files
 	if len(missing) > 0 {
diff --git a/cmd/gdpr_export/main.go b/cmd/gdpr_export/main.go
index f5baa6b3a3f7d387e20cd512c5c9e4d30b56cc81..1039c7ceb33516c4351f40e00bb7f26c72d93bbc 100644
--- a/cmd/gdpr_export/main.go
+++ b/cmd/gdpr_export/main.go
@@ -35,6 +35,8 @@ func main() {
 
 	config.Runtime.IsImportProcess = true // prevents us from creating media by accident
 	config.Path = *configPath
+
+	defer assets.Cleanup()
 	assets.SetupMigrations(*migrationsPath)
 	assets.SetupTemplates(*templatesPath)
 
diff --git a/cmd/gdpr_import/main.go b/cmd/gdpr_import/main.go
index 3cc91c08bb7eca55af947b95dbc95007907e2c1f..9ac4f720e3d7fa6b03d86ca5a2f62ed0424d802c 100644
--- a/cmd/gdpr_import/main.go
+++ b/cmd/gdpr_import/main.go
@@ -32,6 +32,8 @@ func main() {
 
 	config.Runtime.IsImportProcess = true // prevents us from creating media by accident
 	config.Path = *configPath
+
+	defer assets.Cleanup()
 	assets.SetupMigrations(*migrationsPath)
 
 	if ids.GetMachineId() == 0 {
diff --git a/cmd/import_synapse/main.go b/cmd/import_synapse/main.go
index 3d9430ef581a7e524bb96dcaa3e1e87a53cfd299..e3eee8346aa6f71c944c410eabc255a89832b261 100644
--- a/cmd/import_synapse/main.go
+++ b/cmd/import_synapse/main.go
@@ -47,6 +47,8 @@ func main() {
 
 	config.Runtime.IsImportProcess = true // prevents us from creating media by accident
 	config.Path = *configPath
+
+	defer assets.Cleanup()
 	assets.SetupMigrations(*migrationsPath)
 
 	if ids.GetMachineId() == 0 {
@@ -145,9 +147,6 @@ func main() {
 		time.Sleep(1 * time.Second)
 	}
 
-	// Clean up
-	assets.Cleanup()
-
 	logrus.Info("Import completed")
 }
 
diff --git a/cmd/media_repo/main.go b/cmd/media_repo/main.go
index 970b1f8244f947e5be0725fb71732c8c1876e45f..88c1509f6ce0902e543c55844f4a1e46a14bbc71 100644
--- a/cmd/media_repo/main.go
+++ b/cmd/media_repo/main.go
@@ -7,6 +7,7 @@ import (
 	"os/signal"
 	"time"
 
+	"github.com/fsnotify/fsnotify"
 	"github.com/getsentry/sentry-go"
 	"github.com/sirupsen/logrus"
 	"github.com/turt2live/matrix-media-repo/api"
@@ -15,7 +16,6 @@ import (
 	"github.com/turt2live/matrix-media-repo/common/logging"
 	"github.com/turt2live/matrix-media-repo/common/runtime"
 	"github.com/turt2live/matrix-media-repo/common/version"
-	"github.com/turt2live/matrix-media-repo/internal_cache"
 	"github.com/turt2live/matrix-media-repo/metrics"
 	"github.com/turt2live/matrix-media-repo/tasks"
 )
@@ -39,7 +39,6 @@ func main() {
 		configPath = &configEnv
 	}
 
-	version.SetDefaults()
 	config.Path = *configPath
 	if config.Get().Sentry.Enabled {
 		logrus.Info("Setting up Sentry for debugging...")
@@ -56,6 +55,7 @@ func main() {
 	defer sentry.Flush(2 * time.Second)
 	defer sentry.Recover()
 
+	defer assets.Cleanup()
 	assets.SetupMigrations(*migrationsPath)
 	assets.SetupTemplates(*templatesPath)
 	assets.SetupAssets(*assetsPath)
@@ -72,7 +72,6 @@ func main() {
 
 	logrus.Info("Starting up...")
 	runtime.RunStartupSequence()
-	internal_cache.ReplaceInstance() // init the cache as we may be using Redis, and it'd be good to get going sooner
 
 	logrus.Info("Checking background tasks...")
 	err = scanAndStartUnfinishedTasks()
@@ -86,7 +85,9 @@ func main() {
 
 	logrus.Info("Starting config watcher...")
 	watcher := config.Watch()
-	defer watcher.Close()
+	defer func(watcher *fsnotify.Watcher) {
+		_ = watcher.Close()
+	}(watcher)
 	setupReloads()
 
 	logrus.Info("Starting media repository...")
@@ -130,9 +131,6 @@ func main() {
 		stopAllButWeb()
 	}
 
-	// Clean up
-	assets.Cleanup()
-
 	// For debugging
 	logrus.Info("Goodbye!")
 }
diff --git a/cmd/s3_consistency_check/main.go b/cmd/s3_consistency_check/main.go
index 508172410ae0e8522fecad97e0f82c340b914579..3cf5b4bfe25b85e431953968567c661e4f5c32cf 100644
--- a/cmd/s3_consistency_check/main.go
+++ b/cmd/s3_consistency_check/main.go
@@ -32,6 +32,8 @@ func main() {
 
 	config.Runtime.IsImportProcess = true // prevents us from creating media by accident
 	config.Path = *configPath
+
+	defer assets.Cleanup()
 	assets.SetupMigrations(*migrationsPath)
 
 	var err error
diff --git a/common/assets/process.go b/common/assets/process.go
index 947d869ea8aa3bfe5d72269f7c0344ff38f397f9..d32a6ea5f2f3dbf39d85d8ce9ef267782b6db7a9 100644
--- a/common/assets/process.go
+++ b/common/assets/process.go
@@ -16,6 +16,7 @@ import (
 
 var tempMigrations string
 var tempTemplates string
+var tempAssets string
 
 func SetupMigrations(givenMigrationsPath string) {
 	_, err := os.Stat(givenMigrationsPath)
@@ -55,7 +56,7 @@ func SetupAssets(givenAssetsPath string) {
 	_, err := os.Stat(givenAssetsPath)
 	exists := err == nil || !os.IsNotExist(err)
 	if !exists {
-		tempAssets, err := os.MkdirTemp(os.TempDir(), "media-repo-assets")
+		tempAssets, err = os.MkdirTemp(os.TempDir(), "media-repo-assets")
 		if err != nil {
 			panic(err)
 		}
@@ -76,6 +77,10 @@ func Cleanup() {
 		logrus.Info("Cleaning up temporary assets directory: ", tempTemplates)
 		os.Remove(tempTemplates)
 	}
+	if tempAssets != "" {
+		logrus.Info("Cleaning up temporary assets directory: ", tempAssets)
+		os.Remove(tempAssets)
+	}
 }
 
 func extractPrefixTo(pathName string, destination string) {
diff --git a/tasks/purge_remote_media.go b/tasks/purge_remote_media.go
index e8bc160b90eb5d2550001d2a254f9f1bceeff047..95da45282065fe2e0d070cad809b8736ebba07ec 100644
--- a/tasks/purge_remote_media.go
+++ b/tasks/purge_remote_media.go
@@ -1,10 +1,11 @@
 package tasks
 
 import (
-	"github.com/getsentry/sentry-go"
 	"math/rand"
 	"time"
 
+	"github.com/getsentry/sentry-go"
+
 	"github.com/sirupsen/logrus"
 	"github.com/turt2live/matrix-media-repo/common/config"
 	"github.com/turt2live/matrix-media-repo/common/rcontext"