diff --git a/glnx-fdio.c b/glnx-fdio.c
index 62371d028519a5e20d274231eacdd9048d9afbc3..cdbb69fe1abb8c460c99240d87ffc0b4233ac4f0 100644
--- a/glnx-fdio.c
+++ b/glnx-fdio.c
@@ -746,6 +746,8 @@ glnx_file_replace_contents_with_perms_at (int                   dfd,
 
   ret = TRUE;
  out:
+  if (!ret)
+    (void) unlink (tmppath);
   return ret;
 }