diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index 2af6a2dd1b99dac4b04a8128d635bccd0de1cbc1..3df7f09abc7f7f0cfe4e0705dcaa5a924d65941a 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -496,6 +496,7 @@ func setup() {
 		xremoveall(p)
 	}
 	xmkdirall(p)
+	xatexit(func() { xremoveall(p) })
 
 	// Create tool directory.
 	// We keep it in pkg/, just like the object directory above.
diff --git a/src/cmd/dist/buildtool.go b/src/cmd/dist/buildtool.go
index 26e12991a4808758b1dabde02534e471cacf5b1d..190b592b6ef19c85af4c3acaed99b20eb33a4b20 100644
--- a/src/cmd/dist/buildtool.go
+++ b/src/cmd/dist/buildtool.go
@@ -128,6 +128,7 @@ func bootstrapBuildTools() {
 	// but it is easier to debug on failure if the files are in a known location.
 	workspace := pathf("%s/pkg/bootstrap", goroot)
 	xremoveall(workspace)
+	xatexit(func() { xremoveall(workspace) })
 	base := pathf("%s/src/bootstrap", workspace)
 	xmkdirall(base)