diff --git a/config/Config-devel.in b/config/Config-devel.in
index 938f0b3c9cd9cebe79c35b1ca818c95d4b693d2d..d953e6679a8f861e9ef7396d08ec515b1ae7cf8c 100644
--- a/config/Config-devel.in
+++ b/config/Config-devel.in
@@ -39,6 +39,14 @@ menuconfig DEVEL
 		help
 		  Automatically rebuild packages when their files change.
 
+	config AUTOREMOVE
+		bool "Automatic removal of build directories" if DEVEL
+		default n
+		help
+		  Automatically delete build directories after make target completed.
+		  This allows you to symlink build_dir into a scratch location, e.g. a ramdisk,
+		  which does not have enough space to keep a complete build_dir.
+
 	config BUILD_SUFFIX
 		string "Build suffix to append to the target BUILD_DIR variable" if DEVEL
 		default ""
diff --git a/include/package.mk b/include/package.mk
index 37cea1448df8339f9eb2d325bbac70b04dc537ad..b88f2c52b0e54ab0d686ea679c665d34401788f7 100644
--- a/include/package.mk
+++ b/include/package.mk
@@ -211,6 +211,12 @@ define Build/CoreTargets
   .configure: $(STAMP_CONFIGURED)
   .dist: $(STAMP_CONFIGURED)
   .distcheck: $(STAMP_CONFIGURED)
+
+  ifneq ($(CONFIG_AUTOREMOVE),)
+    compile:
+		$(FIND) $(PKG_BUILD_DIR) -mindepth 1 -maxdepth 1 -not '(' -type f -and -name '.*' -and -size 0 ')' -and -not -name '.pkgdir' | \
+			$(XARGS) rm -rf
+  endif
 endef
 
 define Build/DefaultTargets
diff --git a/include/subdir.mk b/include/subdir.mk
index 7e0c474035cb9416b72d5797cc162c87c49cf6a9..e76a01e18ab885466b45734831c3c737c4c823d0 100644
--- a/include/subdir.mk
+++ b/include/subdir.mk
@@ -29,6 +29,11 @@ endef
 lastdir=$(word $(words $(subst /, ,$(1))),$(subst /, ,$(1)))
 diralias=$(if $(findstring $(1),$(call lastdir,$(1))),,$(call lastdir,$(1)))
 
+subdir_make_opts = \
+	-r -C $(1) \
+		BUILD_SUBDIR="$(1)" \
+		BUILD_VARIANT="$(4)"
+
 # 1: subdir
 # 2: target
 # 3: build type
@@ -38,11 +43,19 @@ log_make = \
 	 $(if $(BUILD_LOG), \
 		set -o pipefail; \
 		mkdir -p $(BUILD_LOG_DIR)/$(1)$(if $(4),/$(4));) \
-	$$(SUBMAKE) -r -C $(1) $(if $(3),$(3)-)$(2) \
-		BUILD_SUBDIR="$(1)" \
-		BUILD_VARIANT="$(4)" \
+	$$(SUBMAKE) $(subdir_make_opts) $(if $(3),$(3)-)$(2) \
 		$(if $(BUILD_LOG),SILENT= 2>&1 | tee $(BUILD_LOG_DIR)/$(1)$(if $(4),/$(4))/$(if $(3),$(3)-)$(2).txt)
 
+ifdef CONFIG_AUTOREMOVE
+rebuild_check = \
+	@-$$(NO_TRACE_MAKE) $(subdir_make_opts) check-depends >/dev/null 2>/dev/null; \
+		$(if $(BUILD_LOG),mkdir -p $(BUILD_LOG_DIR)/$(1)$(if $(4),/$(4));) \
+		$$(NO_TRACE_MAKE) $(if $(BUILD_LOG),-d) -q $(subdir_make_opts) .$(if $(3),$(3)-)$(2) \
+			> $(if $(BUILD_LOG),$(BUILD_LOG_DIR)/$(1)$(if $(4),/$(4))/check-$(if $(3),$(3)-)$(2).txt,/dev/null) 2>&1 || \
+			$$(SUBMAKE) $(subdir_make_opts) clean >/dev/null 2>/dev/null
+
+endif
+
 # Parameters: <subdir>
 define subdir
   $(call warn,$(1),d,D $(1))
@@ -58,6 +71,7 @@ define subdir
       $(call warn_eval,$(1)/$(bd),t,T,$(1)/$(bd)/$(target): $(if $(QUILT),,$($(1)/$(bd)/$(target)) $(call $(1)//$(target),$(1)/$(bd))))
         $(foreach variant,$(if $(BUILD_VARIANT),$(BUILD_VARIANT),$(if $(strip $($(1)/$(bd)/variants)),$($(1)/$(bd)/variants),$(if $($(1)/$(bd)/default-variant),$($(1)/$(bd)/default-variant),__default))),
 			$(if $(BUILD_LOG),@mkdir -p $(BUILD_LOG_DIR)/$(1)/$(bd)/$(filter-out __default,$(variant)))
+			$(if $($(1)/autoremove),$(call rebuild_check,$(1)/$(bd),$(target),,$(filter-out __default,$(variant))))
 			$(call log_make,$(1)/$(bd),$(target),,$(filter-out __default,$(variant))) \
 				$(if $(findstring $(bd),$($(1)/builddirs-ignore-$(target))), || $(call ERROR,$(1),   ERROR: $(1)/$(bd) failed to build$(if $(filter-out __default,$(variant)), (build variant: $(variant))).))
         )
diff --git a/package/Makefile b/package/Makefile
index 3250e7b7d0a8c9184893763a4902107acf246096..3cf500d46166a08f90bbac4d57aea280287361b6 100644
--- a/package/Makefile
+++ b/package/Makefile
@@ -10,6 +10,7 @@ curdir:=package
 include $(INCLUDE_DIR)/rootfs.mk
 
 -include $(TMP_DIR)/.packagedeps
+$(curdir)/autoremove:=1
 $(curdir)/builddirs:=$(sort $(package-) $(package-y) $(package-m))
 $(curdir)/builddirs-install:=.
 $(curdir)/builddirs-default:=. $(sort $(package-y) $(package-m))