diff --git a/Library/Homebrew/cmd/uninstall.rb b/Library/Homebrew/cmd/uninstall.rb
index 8c40d4219d..e8e1bb386c 100644
--- a/Library/Homebrew/cmd/uninstall.rb
+++ b/Library/Homebrew/cmd/uninstall.rb
@@ -50,6 +50,11 @@ module Homebrew
all_kegs: args.force?,
)
+ # If ignore_unavailable is true and the named args
+ # are a series of invalid kegs and casks,
+ # #to_kegs_to_casks will return empty arrays.
+ return if all_kegs.blank? && casks.blank?
+
kegs_by_rack = all_kegs.group_by(&:rack)
Uninstall.uninstall_kegs(
@@ -73,5 +78,9 @@ module Homebrew
force: args.force?,
)
end
+
+ return unless Homebrew::EnvConfig.uninstall_autoremove?
+
+ system HOMEBREW_BREW_FILE, "autoremove"
end
end
diff --git a/Library/Homebrew/env_config.rb b/Library/Homebrew/env_config.rb
index 6397f0abb4..115b292a97 100644
--- a/Library/Homebrew/env_config.rb
+++ b/Library/Homebrew/env_config.rb
@@ -343,6 +343,10 @@ module Homebrew
default_text: "macOS: `/private/tmp`, Linux: `/tmp`.",
default: HOMEBREW_DEFAULT_TEMP,
},
+ HOMEBREW_UNINSTALL_AUTOREMOVE: {
+ description: "If set, `brew autoremove` is run after every successful call to `brew uninstall`.",
+ boolean: true,
+ },
HOMEBREW_UPDATE_REPORT_ALL_FORMULAE: {
description: "If set, `brew update` lists changes to all formulae and cask files rather than only showing " \
"when they are new and not installed or outdated and installed.",
diff --git a/Library/Homebrew/sorbet/rbi/hidden-definitions/hidden.rbi b/Library/Homebrew/sorbet/rbi/hidden-definitions/hidden.rbi
index ba6af6ace7..271c840e99 100644
--- a/Library/Homebrew/sorbet/rbi/hidden-definitions/hidden.rbi
+++ b/Library/Homebrew/sorbet/rbi/hidden-definitions/hidden.rbi
@@ -2572,6 +2572,8 @@ module Homebrew::EnvConfig
def self.temp(); end
+ def self.uninstall_autoremove?(); end
+
def self.update_report_all_formulae?(); end
def self.update_to_tag?(); end
diff --git a/docs/Manpage.md b/docs/Manpage.md
index f74003edb1..bd9abaa82a 100644
--- a/docs/Manpage.md
+++ b/docs/Manpage.md
@@ -2184,6 +2184,9 @@ example, run `export HOMEBREW_NO_INSECURE_REDIRECT=1` rather than just
*Default:* macOS: `/private/tmp`, Linux: `/tmp`.
+- `HOMEBREW_UNINSTALL_AUTOREMOVE`
+
If set, `brew autoremove` is run after every successful call to `brew uninstall`.
+
- `HOMEBREW_UPDATE_REPORT_ALL_FORMULAE`
If set, `brew update` lists changes to all formulae and cask files rather than only showing when they are new and not installed or outdated and installed.
diff --git a/manpages/brew.1 b/manpages/brew.1
index 87c2999563..d340397ea2 100644
--- a/manpages/brew.1
+++ b/manpages/brew.1
@@ -3207,6 +3207,12 @@ Use this path as the temporary directory for building packages\. Changing this m
\fIDefault:\fR macOS: \fB/private/tmp\fR, Linux: \fB/tmp\fR\.
.
.TP
+\fBHOMEBREW_UNINSTALL_AUTOREMOVE\fR
+.
+.br
+If set, \fBbrew autoremove\fR is run after every successful call to \fBbrew uninstall\fR\.
+.
+.TP
\fBHOMEBREW_UPDATE_REPORT_ALL_FORMULAE\fR
.
.br