diff --git a/htools/Ganeti/Daemon.hs b/htools/Ganeti/Daemon.hs
index d6ce50b4001809b5085c53d5efff12eb19571d64..80f74f143403a8450e2fbe069be39c714c3636f2 100644
--- a/htools/Ganeti/Daemon.hs
+++ b/htools/Ganeti/Daemon.hs
@@ -37,6 +37,7 @@ module Ganeti.Daemon
   , oSyslogUsage
   , parseArgs
   , parseAddress
+  , cleanupSocket
   , writePidFile
   , genericMain
   ) where
@@ -54,6 +55,7 @@ import System.Exit
 import System.Environment
 import System.Info
 import System.IO
+import System.IO.Error (isDoesNotExistError)
 import System.Posix.Directory
 import System.Posix.Files
 import System.Posix.IO
@@ -221,6 +223,13 @@ writePidFile path = do
   catch (fmap Ok $ _writePidFile path)
     (return . Bad . formatIOError "Failure during writing of the pid file")
 
+-- | Helper function to ensure a socket doesn't exist. Should only be
+-- called once we have locked the pid file successfully.
+cleanupSocket :: FilePath -> IO ()
+cleanupSocket socketPath = do
+  catchJust (guard . isDoesNotExistError) (removeLink socketPath)
+            (const $ return ())
+
 -- | Sets up a daemon's environment.
 setupDaemonEnv :: FilePath -> FileMode -> IO ()
 setupDaemonEnv cwd umask = do
diff --git a/htools/Ganeti/Queryd.hs b/htools/Ganeti/Queryd.hs
index eeeef7ac62e70974422cf5b18f8740d577432e53..c7d417a7e8f80353b86a94dfe04b662c92a65f84 100644
--- a/htools/Ganeti/Queryd.hs
+++ b/htools/Ganeti/Queryd.hs
@@ -26,8 +26,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 -}
 
 module Ganeti.Queryd
-
-where
+  ( ConfigReader
+  , runQueryD
+  ) where
 
 import Control.Applicative
 import Control.Concurrent
@@ -41,6 +42,7 @@ import Text.JSON.Pretty (pp_value)
 import System.Info (arch)
 
 import qualified Ganeti.Constants as C
+import Ganeti.Daemon
 import Ganeti.Objects
 import qualified Ganeti.Config as Config
 import Ganeti.BasicTypes
@@ -183,6 +185,7 @@ mainLoop creader socket = do
 runQueryD :: Maybe FilePath -> ConfigReader -> IO ()
 runQueryD fpath creader = do
   let socket_path = fromMaybe C.querySocket fpath
+  cleanupSocket socket_path
   bracket
     (getServer socket_path)
     (closeServer socket_path)