Class: Homebrew::Cleanup Private
- Extended by:
- Utils::Output::Mixin, Utils::Path
- Defined in:
- cleanup.rb
Overview
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
Helper class for cleaning up the Homebrew cache.
Constant Summary collapse
- PERIODIC_CLEAN_FILE =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
T.let((HOMEBREW_CACHE/".cleaned").freeze, Pathname)
Instance Attribute Summary collapse
- #args ⇒ Array<String> readonly private
- #cache ⇒ Pathname readonly private
- #days ⇒ Integer readonly private
- #disk_cleanup_size ⇒ Integer readonly private
Class Method Summary collapse
- .autoremove(dry_run: false, removed_kegs: [], removed_casks: []) ⇒ void private
- .cask_cache_file_current?(pathname, cask, name) ⇒ Boolean private
- .content_addressed_cache?(pathname) ⇒ Boolean private
- .dry_run_output(*args, formulae: [], quiet: false) ⇒ String private
- .go_cache_directory?(pathname) ⇒ Boolean private
- .incomplete?(pathname) ⇒ Boolean private
- .install_clean!(formulae: [], casks: []) ⇒ void private
- .install_cleanup_formulae(formulae) ⇒ Array<Formula> private
- .install_formula_clean!(formula) ⇒ void private
- .nested_cache?(pathname) ⇒ Boolean private
- .periodic_clean!(dry_run: false) ⇒ void private
- .periodic_clean_due? ⇒ Boolean private
- .printed_dry_run_output?(output, ohai: false) ⇒ Boolean private
- .prune?(pathname, days) ⇒ Boolean private
- .puts_no_install_cleanup_disable_message ⇒ void private
- .puts_no_install_cleanup_disable_message_if_not_already! ⇒ void private
- .skip_clean_formula?(formula) ⇒ Boolean private
- .stale?(entry, scrub: false) ⇒ Boolean private
- .stale_cask_download?(pathname, cask, name, scrub:) ⇒ Boolean private
Instance Method Summary collapse
- #cache_entries(paths, type:) ⇒ Array<Hash> private
- #cache_files ⇒ Array<Hash> private
- #clean!(quiet: false, periodic: false) ⇒ void private
- #cleanup_bootsnap ⇒ void private
- #cleanup_cache(entries = nil, cleanup_unreferenced: true) ⇒ void private
- #cleanup_cache_db(rack = nil) ⇒ void private
- #cleanup_cache_entries(paths, type:, cleanup_unreferenced: true) ⇒ void private
- #cleanup_cask(cask, ds_store: true, cleanup_legacy_downloads: true, cleanup_unreferenced: true) ⇒ void private
- #cleanup_empty_api_source_directories(directory = cache/"api-source") ⇒ void private
- #cleanup_formula(formula, quiet: false, ds_store: true, cache_db: true, cleanup_unreferenced: true) ⇒ void private
- #cleanup_keg(keg) ⇒ void private
-
#cleanup_legacy_cask_downloads(casks) ⇒ void
private
Added 2026-07-05 for legacy cask cache symlinks named after URL basenames.
- #cleanup_lockfiles(*lockfiles) ⇒ void private
- #cleanup_logs ⇒ void private
- #cleanup_path(path, &_block) ⇒ void private
- #cleanup_portable_ruby ⇒ void private
- #cleanup_python_site_packages ⇒ void private
- #cleanup_reinstall_kegs ⇒ void private
- #cleanup_temp_staging ⇒ void private
- #cleanup_unreferenced_downloads ⇒ void private
- #dry_run? ⇒ Boolean private
-
#formula_cache_paths(formula) ⇒ Array<Pathname>
private
Returns the cached
<formula>--<version>and<formula>_bottle_manifest--<version>downloads for a formula. - #initialize(*args, dry_run: false, scrub: false, days: nil, cache: HOMEBREW_CACHE, output: $stdout) ⇒ void constructor private
- #prune? ⇒ Boolean private
- #prune_content_addressed_cache(path) ⇒ void private
- #prune_prefix_symlinks_and_directories ⇒ void private
- #rm_ds_store(dirs = nil) ⇒ void private
- #scrub? ⇒ Boolean private
- #unremovable_kegs ⇒ Array<Keg> private
- #use_system_ruby? ⇒ Boolean private
Methods included from Utils::Output::Mixin
issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_once, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_removed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning
Methods included from Utils::Path
formula_installed_prefixes, formula_opt_bin_env, formula_opt_bin_path, install_info, loadable_package_path?, tap_package_path, uninstall_info, valid_package_name?
Methods included from Utils::Path::Helpers
#child_of?, #cp_path_sub, #ensure_child_of!, #ensure_writable, #relative_path_from, #resolved_path, #resolved_path_exists?, #rmdir_if_possible, #text_executable?
Methods included from Utils::Path::FormulaHelpers
#formula_any_version_installed?, #formula_opt_bin, #formula_opt_include, #formula_opt_lib, #formula_opt_libexec, #formula_opt_prefix
Constructor Details
#initialize(*args, dry_run: false, scrub: false, days: nil, cache: HOMEBREW_CACHE, output: $stdout) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'cleanup.rb', line 293 def initialize(*args, dry_run: false, scrub: false, days: nil, cache: HOMEBREW_CACHE, output: $stdout) @disk_cleanup_size = T.let(0, Integer) @args = args @dry_run = dry_run @scrub = scrub @prune = T.let(days.present?, T::Boolean) @days = T.let(days || Homebrew::EnvConfig.cleanup_max_age_days.to_i, Integer) @cache = cache @output = output @cleaned_up_paths = T.let(Set.new, T::Set[Pathname]) @formula_cache_paths = T.let(nil, T.nilable(T::Hash[String, T::Array[Pathname]])) end |
Instance Attribute Details
#args ⇒ Array<String> (readonly)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
272 273 274 |
# File 'cleanup.rb', line 272 def args @args end |
#cache ⇒ Pathname (readonly)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
278 279 280 |
# File 'cleanup.rb', line 278 def cache @cache end |
#days ⇒ Integer (readonly)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
275 276 277 |
# File 'cleanup.rb', line 275 def days @days end |
#disk_cleanup_size ⇒ Integer (readonly)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
281 282 283 |
# File 'cleanup.rb', line 281 def disk_cleanup_size @disk_cleanup_size end |
Class Method Details
.autoremove(dry_run: false, removed_kegs: [], removed_casks: []) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 |
# File 'cleanup.rb', line 1060 def self.autoremove(dry_run: false, removed_kegs: [], removed_casks: []) require "utils/autoremove" require "cask/caskroom" # If this runs after install, uninstall, reinstall or upgrade, # the cache of installed formulae may no longer be valid. Formula.clear_cache unless dry_run installed_formulae = Formula.installed unless removed_kegs.empty? installed_formulae = installed_formulae.reject do |formula| formula.installed_kegs.all? { |keg| removed_kegs.include?(keg) } end end formulae = installed_formulae # Remove formulae listed in HOMEBREW_NO_CLEANUP_FORMULAE and their dependencies. if Homebrew::EnvConfig.no_cleanup_formulae.present? formulae -= formulae.select { skip_clean_formula?(it) } .flat_map { |f| [f, *f.installed_runtime_formula_dependencies] } end casks = Cask::Caskroom.casks if removed_casks.present? casks = casks.reject do |cask| removed_casks.any? { |removed_cask| removed_cask.token == cask.token } end end removable_formulae = Utils::Autoremove.removable_formulae(formulae, casks) if (candidate_kegs = removable_formulae.filter_map(&:any_installed_keg).presence) && (required_kegs, = InstalledDependents.find_some_installed_dependents(candidate_kegs, casks: removed_casks, formulae: installed_formulae)) && (required_names = Set.new(required_kegs.map(&:name)).presence) removable_formulae.reject! { |formula| required_names.include?(formula.name) } end removable_formulae = Utils::Autoremove.formulae_safe_to_remove(removable_formulae, installed_formulae) return if removable_formulae.blank? formulae_names = removable_formulae.map(&:full_name).sort verb = dry_run ? "Would autoremove" : "Autoremoving" oh1 "#{verb} #{formulae_names.count} unneeded #{Utils.pluralize("formula", formulae_names.count)}:" puts formulae_names.join("\n") return if dry_run require "uninstall" kegs_by_rack = removable_formulae.filter_map(&:any_installed_keg).group_by(&:rack) Uninstall.uninstall_kegs(kegs_by_rack) # The installed formula cache will be invalid after uninstalling. Formula.clear_cache end |
.cask_cache_file_current?(pathname, cask, name) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
103 104 105 106 107 108 |
# File 'cleanup.rb', line 103 def cask_cache_file_current?(pathname, cask, name) # Casks that only define a version inside OS-specific blocks have none on other systems. return false unless (version = cask.version) pathname.basename.to_s.match?(/\A#{Regexp.escape(name)}--#{Regexp.escape(version)}(?:\.|\z)/) end |
.content_addressed_cache?(pathname) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
61 62 63 64 |
# File 'cleanup.rb', line 61 def content_addressed_cache?(pathname) pathname.directory? && Homebrew::PackageManagerCache::CONTENT_ADDRESSED_DIRECTORIES.include?(pathname.basename.to_s) end |
.dry_run_output(*args, formulae: [], quiet: false) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'cleanup.rb', line 330 def self.dry_run_output(*args, formulae: [], quiet: false) output = StringIO.new old_stdout = $stdout begin $stdout = output cleanup = Cleanup.new(*args, dry_run: true) if formulae.empty? cleanup.clean!(quiet:) else formulae.each { |formula| cleanup.cleanup_formula(formula, quiet:) } end ensure $stdout = old_stdout end output.string end |
.go_cache_directory?(pathname) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
67 68 69 70 71 |
# File 'cleanup.rb', line 67 def go_cache_directory?(pathname) # Go makes its cache contents read-only to ensure cache integrity, # which makes sense but is something we need to undo for cleanup. pathname.directory? && %w[go_cache go_mod_cache].include?(pathname.basename.to_s) end |
.incomplete?(pathname) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
51 52 53 |
# File 'cleanup.rb', line 51 def incomplete?(pathname) pathname.extname.end_with?(".incomplete") end |
.install_clean!(formulae: [], casks: []) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'cleanup.rb', line 370 def self.install_clean!(formulae: [], casks: []) return if Homebrew::EnvConfig.no_install_cleanup? formulae = install_cleanup_formulae(formulae).uniq(&:full_name) casks = casks.uniq(&:full_name) packages = T.let(formulae + casks, T::Array[T.any(Formula, Cask::Cask)]) return if packages.empty? output = InstallCleanupOutput.new($stdout, oh1_title("Cleanup")) Utils.parallel_map(packages) do |package| cleanup = Cleanup.new(output:) case package when Formula cleanup.cleanup_formula(package, quiet: true, cache_db: false, cleanup_unreferenced: false) when Cask::Cask cleanup.cleanup_cask(package, cleanup_unreferenced: false) else T.absurd(package) end end Cleanup.new.cleanup_cache_db if formulae.present? Cleanup.new.cleanup_unreferenced_downloads return unless output.printed? end |
.install_cleanup_formulae(formulae) ⇒ Array<Formula>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
348 349 350 351 352 353 354 |
# File 'cleanup.rb', line 348 def self.install_cleanup_formulae(formulae) return [] if Homebrew::EnvConfig.no_install_cleanup? formulae.select do |formula| formula.latest_version_installed? && !skip_clean_formula?(formula) end end |
.install_formula_clean!(formula) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
357 358 359 360 361 362 363 364 365 366 367 |
# File 'cleanup.rb', line 357 def self.install_formula_clean!(formula) return if install_cleanup_formulae([formula]).blank? output = StringIO.new Cleanup.new(output:).cleanup_formula(formula, quiet: true) return if output.string.empty? ohai "Running `brew cleanup #{formula}`..." print output.string end |
.nested_cache?(pathname) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
56 57 58 |
# File 'cleanup.rb', line 56 def nested_cache?(pathname) pathname.directory? && Homebrew::PackageManagerCache::DIRECTORIES.include?(pathname.basename.to_s) end |
.periodic_clean!(dry_run: false) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'cleanup.rb', line 439 def self.periodic_clean!(dry_run: false) return if Homebrew::EnvConfig.no_install_cleanup? return unless periodic_clean_due? if dry_run oh1 "Would run `brew cleanup` which has not been run in the last #{CLEANUP_DEFAULT_DAYS} days" else oh1 "`brew cleanup` has not been run in the last #{CLEANUP_DEFAULT_DAYS} days, running now..." end return if dry_run Cleanup.new.clean!(quiet: true, periodic: true) end |
.periodic_clean_due? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
426 427 428 429 430 431 432 433 434 435 436 |
# File 'cleanup.rb', line 426 def self.periodic_clean_due? return false if Homebrew::EnvConfig.no_install_cleanup? unless PERIODIC_CLEAN_FILE.exist? HOMEBREW_CACHE.mkpath FileUtils.touch PERIODIC_CLEAN_FILE return false end PERIODIC_CLEAN_FILE.mtime < (DateTime.now - CLEANUP_DEFAULT_DAYS).to_time end |
.printed_dry_run_output?(output, ohai: false) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'cleanup.rb', line 316 def self.printed_dry_run_output?(output, ohai: false) return false if output.blank? if ohai ohai "Would `brew cleanup`" else puts "Would `brew cleanup`:" end print output puts unless output.end_with?("\n") true end |
.prune?(pathname, days) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
74 75 76 77 78 79 80 81 |
# File 'cleanup.rb', line 74 def prune?(pathname, days) return false unless days return true if days.zero? return true if pathname.symlink? && !pathname.exist? days_ago = (DateTime.now - days).to_time pathname.mtime < days_ago && pathname.ctime < days_ago end |
.puts_no_install_cleanup_disable_message ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
400 401 402 403 404 405 406 |
# File 'cleanup.rb', line 400 def self. return if Homebrew::EnvConfig.no_env_hints? return if Homebrew::EnvConfig.no_install_cleanup? puts "Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`." puts "Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`)." end |
.puts_no_install_cleanup_disable_message_if_not_already! ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
409 410 411 412 413 414 |
# File 'cleanup.rb', line 409 def self. return if @puts_no_install_cleanup_disable_message_if_not_already @puts_no_install_cleanup_disable_message_if_not_already = T.let(true, T.nilable(TrueClass)) end |
.skip_clean_formula?(formula) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
417 418 419 420 421 422 423 |
# File 'cleanup.rb', line 417 def self.skip_clean_formula?(formula) no_cleanup_formula = Homebrew::EnvConfig.no_cleanup_formulae return false if no_cleanup_formula.blank? @skip_clean_formulae ||= T.let(no_cleanup_formula.split(","), T.nilable(T::Array[String])) @skip_clean_formulae.include?(formula.name) || @skip_clean_formulae.intersect?(formula.aliases) end |
.stale?(entry, scrub: false) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'cleanup.rb', line 84 def stale?(entry, scrub: false) pathname = entry[:path] return false unless resolved_path(pathname).file? case entry[:type] when :api_package scrub when :api_source stale_api_source?(pathname, scrub) when :cask stale_cask?(pathname, scrub) when :gh_actions_artifact scrub || prune?(pathname, GH_ACTIONS_ARTIFACT_CLEANUP_DAYS) else stale_formula?(pathname, scrub) end end |
.stale_cask_download?(pathname, cask, name, scrub:) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'cleanup.rb', line 111 def stale_cask_download?(pathname, cask, name, scrub:) return true unless pathname.exist? return true unless cask_cache_file_current?(pathname, cask, name) return true if scrub && cask.installed_version != cask.version if cask.version.latest? cleanup_threshold = (DateTime.now - CLEANUP_DEFAULT_DAYS).to_time return pathname.mtime < cleanup_threshold && pathname.ctime < cleanup_threshold end false end |
Instance Method Details
#cache_entries(paths, type:) ⇒ Array<Hash>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
539 540 541 |
# File 'cleanup.rb', line 539 def cache_entries(paths, type:) paths.map { |path| { path:, type: } } end |
#cache_files ⇒ Array<Hash>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
# File 'cleanup.rb', line 667 def cache_files files = cache.directory? ? cache.children : [] cask_files = (cache/"Cask").directory? ? (cache/"Cask").children : [] api_internal = cache/"api/internal" api_package_files = if scrub? && api_internal.directory? current_api_package_basename = Homebrew::API::Internal.cached_packages_json_file_path.basename.to_s # Keep only the current OS's envelope and its sidecars and scrub the # rest, including orphaned sidecars and temp files. # Keep in sync with the previous-OS-version removal in cmd/update.sh. kept_basenames = [ current_api_package_basename, "#{current_api_package_basename}.payload", "#{current_api_package_basename}.payload.index", "#{current_api_package_basename}.etag", "#{current_api_package_basename}.etag.url", ] api_internal.glob("packages.*.jws.json*").reject do |path| kept_basenames.include?(path.basename.to_s) end else [] end api_resource_files = %w[cask formula].flat_map do |resource_type| directory = cache/"api"/resource_type directory.directory? ? directory.children : [] end api_source_files = (cache/"api-source").glob("*/*/*/**/*").select { |path| path.file? || path.symlink? } gh_actions_artifacts = (cache/"gh-actions-artifact").directory? ? (cache/"gh-actions-artifact").children : [] cache_entries(files, type: nil) + cache_entries(cask_files, type: :cask) + cache_entries(api_package_files, type: :api_package) + cache_entries(api_resource_files, type: :api_package) + cache_entries(api_source_files, type: :api_source) + cache_entries(gh_actions_artifacts, type: :gh_actions_artifact) end |
#clean!(quiet: false, periodic: false) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'cleanup.rb', line 456 def clean!(quiet: false, periodic: false) require "cask/cask_loader" if args.empty? Formula.installed .sort_by(&:name) .reject { |f| Cleanup.skip_clean_formula?(f) } .each do |formula| # Don't `cleanup_unreferenced` here for each formula. # Instead, let it be run once `cleanup_cache` below. cleanup_formula(formula, quiet:, ds_store: false, cache_db: false, cleanup_unreferenced: false) end if periodic Cask::Caskroom.casks.sort_by(&:full_name).each do |cask| cleanup_cask(cask, ds_store: false, cleanup_legacy_downloads: false, cleanup_unreferenced: false) end end if ENV["HOMEBREW_AUTOREMOVE"].present? opoo "`$HOMEBREW_AUTOREMOVE` is now a no-op as it is the default behaviour. " \ "Set `HOMEBREW_NO_AUTOREMOVE=1` to disable it." end Cleanup.autoremove(dry_run: dry_run?) unless Homebrew::EnvConfig.no_autoremove? cleanup_cache cleanup_empty_api_source_directories cleanup_bootsnap cleanup_logs cleanup_temp_staging cleanup_reinstall_kegs cleanup_lockfiles cleanup_python_site_packages prune_prefix_symlinks_and_directories unless dry_run? cleanup_cache_db rm_ds_store HOMEBREW_CACHE.mkpath FileUtils.touch PERIODIC_CLEAN_FILE end # Cleaning up Ruby needs to be done last to avoid requiring additional # files afterwards. Additionally, don't allow it on periodic cleans to # avoid having to try to do a `brew install` when we've just deleted # the running Ruby process... return if periodic cleanup_portable_ruby else args.each do |arg| formula = begin Formulary.resolve(arg) rescue FormulaUnavailableError, TapFormulaAmbiguityError nil end cask = begin Cask::CaskLoader.load(arg) rescue Cask::CaskError nil end if formula && Cleanup.skip_clean_formula?(formula) onoe "Refusing to clean #{formula} because it is listed in " \ "#{Tty.bold}HOMEBREW_NO_CLEANUP_FORMULAE#{Tty.reset}!" elsif formula cleanup_formula(formula) end cleanup_cask(cask) if cask end end end |
#cleanup_bootsnap ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
886 887 888 889 890 891 892 893 |
# File 'cleanup.rb', line 886 def cleanup_bootsnap bootsnap = cache/"bootsnap" return unless bootsnap.directory? bootsnap.each_child do |subdir| cleanup_path(subdir) { FileUtils.rm_r(subdir) } if subdir.basename.to_s != Homebrew::Bootsnap.key end end |
#cleanup_cache(entries = nil, cleanup_unreferenced: true) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
# File 'cleanup.rb', line 749 def cleanup_cache(entries = nil, cleanup_unreferenced: true) full_cache_cleanup = entries.nil? entries ||= cache_files entries.each do |entry| path = entry[:path] next if path == PERIODIC_CLEAN_FILE FileUtils.chmod_R 0755, path if self.class.go_cache_directory?(path) && !dry_run? next cleanup_path(path) { path.unlink } if self.class.incomplete?(path) if self.class.nested_cache?(path) # Caches that verify their contents on every read stay safe to reuse # between builds, so only prune their old files rather than wiping them. if self.class.content_addressed_cache?(path) prune_content_addressed_cache(path) else cleanup_path(path) { FileUtils.rm_rf path } end next end if self.class.prune?(path, days) if path.file? || path.symlink? cleanup_path(path) { path.unlink } elsif path.directory? && path.to_s.include?("--") cleanup_path(path) { FileUtils.rm_rf path } end next end # If we've specified --prune don't do the (expensive) .stale? check. cleanup_path(path) { path.unlink } if !prune? && self.class.stale?(entry, scrub: scrub?) end cleanup_legacy_cask_downloads(Cask::Caskroom.casks) if full_cache_cleanup cleanup_unreferenced_downloads if cleanup_unreferenced end |
#cleanup_cache_db(rack = nil) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 |
# File 'cleanup.rb', line 896 def cleanup_cache_db(rack = nil) return if dry_run? FileUtils.rm_rf [ cache/"desc_cache.json", cache/"linkage.db", cache/"linkage.db.db", ] CacheStoreDatabase.use(:linkage) do |db| break unless db.created? db.each_key do |keg| keg = T.cast(keg, String) next if rack && !keg.start_with?("#{rack}/") next if File.directory?(keg) LinkageCacheStore.new( keg, T.cast(db, CacheStoreDatabase[String, T::Hash[T.any(String, Symbol), T.anything]]), ).delete! end end end |
#cleanup_cache_entries(paths, type:, cleanup_unreferenced: true) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
546 547 548 |
# File 'cleanup.rb', line 546 def cleanup_cache_entries(paths, type:, cleanup_unreferenced: true) cleanup_cache(cache_entries(paths, type:), cleanup_unreferenced:) end |
#cleanup_cask(cask, ds_store: true, cleanup_legacy_downloads: true, cleanup_unreferenced: true) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
589 590 591 592 593 594 595 596 |
# File 'cleanup.rb', line 589 def cleanup_cask(cask, ds_store: true, cleanup_legacy_downloads: true, cleanup_unreferenced: true) cleanup_cache_entries(Pathname.glob(cache/"Cask/#{cask.token}--*"), type: :cask, cleanup_unreferenced: false) cleanup_legacy_cask_downloads([cask]) if cleanup_legacy_downloads cleanup_unreferenced_downloads if cleanup_unreferenced rm_ds_store([cask.caskroom_path]) if ds_store cleanup_lockfiles(CaskLock.new(cask.token).path) end |
#cleanup_empty_api_source_directories(directory = cache/"api-source") ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
705 706 707 708 709 710 711 712 713 714 715 |
# File 'cleanup.rb', line 705 def cleanup_empty_api_source_directories(directory = cache/"api-source") return if dry_run? return unless directory.directory? directory.each_child do |child| next unless child.directory? cleanup_empty_api_source_directories(child) child.rmdir if child.empty? end end |
#cleanup_formula(formula, quiet: false, ds_store: true, cache_db: true, cleanup_unreferenced: true) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
572 573 574 575 576 577 578 579 |
# File 'cleanup.rb', line 572 def cleanup_formula(formula, quiet: false, ds_store: true, cache_db: true, cleanup_unreferenced: true) formula.eligible_kegs_for_cleanup(quiet:) .each { |keg| cleanup_keg(keg) } cleanup_cache_entries(formula_cache_paths(formula), type: nil, cleanup_unreferenced:) rm_ds_store([formula.rack]) if ds_store cleanup_cache_db(formula.rack) if cache_db cleanup_lockfiles(FormulaLock.new(formula.name).path) end |
#cleanup_keg(keg) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
626 627 628 629 630 631 |
# File 'cleanup.rb', line 626 def cleanup_keg(keg) cleanup_path(Pathname.new(keg)) { keg.uninstall(raise_failures: true) } rescue Errno::EACCES, Errno::ENOTEMPTY => e opoo e. unremovable_kegs << keg end |
#cleanup_legacy_cask_downloads(casks) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
Added 2026-07-05 for legacy cask cache symlinks named after URL basenames. Remove after 2026-11-02, once the 120-day fallback stale-file sweep has had time to prune stale files created before cask downloads were token-named.
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'cleanup.rb', line 602 def cleanup_legacy_cask_downloads(casks) cask_cache = cache/"Cask" return unless cask_cache.directory? cask_cache_paths = cask_cache.children.select { |path| path.file? || path.symlink? } casks.each do |cask| next unless (url = cask.url) legacy_download_name = Utils.safe_filename(File.basename(url.to_s)) next if legacy_download_name.blank? || legacy_download_name == cask.token cask_cache_paths.each do |path| next unless path.basename.to_s.start_with?("#{legacy_download_name}--") next if !self.class.stale_cask_download?(path, cask, legacy_download_name, scrub: scrub?) && (!self.class.cask_cache_file_current?(path, cask, legacy_download_name) || !(cask_cache/Utils.safe_filename("#{cask.token}--#{cask.version}#{path.extname}")).exist?) cleanup_path(path) { path.unlink } end end end |
#cleanup_lockfiles(*lockfiles) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 |
# File 'cleanup.rb', line 822 def cleanup_lockfiles(*lockfiles) return if dry_run? lockfiles = HOMEBREW_LOCKS.children.select(&:file?) if lockfiles.empty? && HOMEBREW_LOCKS.directory? lockfiles.each do |file| next unless file.readable? file.open(File::RDWR | File::NOFOLLOW) do |lockfile| next unless lockfile.flock(File::LOCK_EX | File::LOCK_NB) next unless File.identical?(file, lockfile) file.unlink end rescue Errno::ELOOP, Errno::ENOENT next end end |
#cleanup_logs ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
634 635 636 637 638 639 640 641 642 |
# File 'cleanup.rb', line 634 def cleanup_logs return unless HOMEBREW_LOGS.directory? logs_days = [days, CLEANUP_DEFAULT_DAYS].min HOMEBREW_LOGS.subdirs.each do |dir| cleanup_path(dir) { FileUtils.rm_r(dir) } if self.class.prune?(dir, logs_days) end end |
#cleanup_path(path, &_block) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
807 808 809 810 811 812 813 814 815 816 817 818 819 |
# File 'cleanup.rb', line 807 def cleanup_path(path, &_block) return if !path.exist? && !path.symlink? return unless @cleaned_up_paths.add?(path) @disk_cleanup_size += path.disk_usage if dry_run? @output.puts "Would remove: #{path} (#{path.abv})" else @output.puts "Removing: #{path}... (#{path.abv})" yield end end |
#cleanup_portable_ruby ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 |
# File 'cleanup.rb', line 842 def cleanup_portable_ruby vendor_dir = HOMEBREW_LIBRARY/"Homebrew/vendor" portable_ruby_latest_version = (vendor_dir/"portable-ruby-version").read.chomp portable_rubies_to_remove = [] Pathname.glob(vendor_dir/"portable-ruby/*.*").select(&:directory?).each do |path| next if !use_system_ruby? && portable_ruby_latest_version == path.basename.to_s portable_rubies_to_remove << path end return if portable_rubies_to_remove.empty? bundler_paths = (vendor_dir/"bundle/ruby").children.select do |child| basename = child.basename.to_s next false if basename == ".homebrew_gem_groups" next true unless child.directory? [ "#{Version.new(portable_ruby_latest_version).major_minor}.0", RbConfig::CONFIG["ruby_version"], ].uniq.exclude?(basename) end bundler_paths.each do |bundler_path| if dry_run? puts Utils.popen_read("git", "-C", HOMEBREW_REPOSITORY, "clean", "-nx", bundler_path).chomp else puts Utils.popen_read("git", "-C", HOMEBREW_REPOSITORY, "clean", "-ffqx", bundler_path).chomp end end portable_rubies_to_remove.each do |portable_ruby| cleanup_path(portable_ruby) { FileUtils.rm_r(portable_ruby) } end end |
#cleanup_python_site_packages ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 |
# File 'cleanup.rb', line 939 def cleanup_python_site_packages pyc_files = Hash.new { |h, k| h[k] = [] } seen_non_pyc_file = Hash.new { |h, k| h[k] = false } unused_pyc_files = [] HOMEBREW_PREFIX.glob("lib/python*/site-packages").each do |site_packages| site_packages.each_child do |child| next unless child.directory? # TODO: Work out a sensible way to clean up `pip`'s, `setuptools`' and `wheel`'s # `{dist,site}-info` directories. Alternatively, consider always removing # all `-info` directories, because we may not be making use of them. next if child.basename.to_s.end_with?("-info") # Clean up old *.pyc files in the top-level __pycache__. if child.basename.to_s == "__pycache__" child.find do |path| next if path.extname != ".pyc" next unless self.class.prune?(path, days) unused_pyc_files << path end next end # Look for directories that contain only *.pyc files. child.find do |path| next if path.directory? if path.extname == ".pyc" pyc_files[child] << path else seen_non_pyc_file[child] = true break end end end end unused_pyc_files += pyc_files.reject { |k,| seen_non_pyc_file[k] } .values .flatten return if unused_pyc_files.blank? unused_pyc_files.each do |pyc| cleanup_path(pyc) { pyc.unlink } end end |
#cleanup_reinstall_kegs ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
658 659 660 661 662 663 664 |
# File 'cleanup.rb', line 658 def cleanup_reinstall_kegs return unless HOMEBREW_CELLAR.directory? HOMEBREW_CELLAR.glob("*/*.reinstall").each do |reinstall_keg| cleanup_path(reinstall_keg) { FileUtils.rm_r(reinstall_keg) } end end |
#cleanup_temp_staging ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
645 646 647 648 649 650 651 652 653 654 655 |
# File 'cleanup.rb', line 645 def cleanup_temp_staging [HOMEBREW_TEMP_CELLAR, HOMEBREW_TEMP_CASKROOM].each do |temp_dir| next unless temp_dir.directory? temp_dir.each_child do |child| cleanup_path(child) { FileUtils.rm_r(child) } rescue Errno::EACCES, Errno::ENOTEMPTY => e opoo e. end end end |
#cleanup_unreferenced_downloads ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
# File 'cleanup.rb', line 718 def cleanup_unreferenced_downloads return if dry_run? return unless (cache/"downloads").directory? downloads = (cache/"downloads").children referenced_downloads = cache_files.map { |file| file[:path] }.select(&:symlink?) .map { |path| resolved_path(path) } (downloads - referenced_downloads).each do |download| if self.class.incomplete?(download) begin DownloadLock.new(download).with_lock do download.unlink end rescue OperationInProgressError # Skip incomplete downloads which are still in progress. next end elsif download.directory? FileUtils.rm_rf download else download.unlink end end end |
#dry_run? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
307 |
# File 'cleanup.rb', line 307 def dry_run? = @dry_run |
#formula_cache_paths(formula) ⇒ Array<Pathname>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the cached <formula>--<version> and
<formula>_bottle_manifest--<version> downloads for a formula. Globbing
these per formula rescans the entire cache each time, which is quadratic
for brew cleanup, so index the cache by name prefix once instead.
555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'cleanup.rb', line 555 def formula_cache_paths(formula) return [] unless cache.directory? index = @formula_cache_paths ||= cache.children.each_with_object({}) do |path, hash| prefix, separator, = path.basename.to_s.partition("--") next if prefix.start_with?(".") || separator.empty? (hash[prefix] ||= []) << path end [*index.fetch(formula.name, []), *index.fetch("#{formula.name}_bottle_manifest", [])].sort end |
#prune? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
310 |
# File 'cleanup.rb', line 310 def prune? = @prune |
#prune_content_addressed_cache(path) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 |
# File 'cleanup.rb', line 789 def prune_content_addressed_cache(path) files = Find.find(path.to_s).filter_map do |file| file = Pathname(file) file if file.file? && (scrub? || self.class.prune?(file, days)) end return if files.empty? @disk_cleanup_size += files.sum(&:disk_usage) count = Utils.pluralize("file", files.count, include_count: true) if dry_run? @output.puts "Would prune #{count} from: #{path}" else @output.puts "Pruning #{count} from: #{path}..." files.each(&:unlink) end end |
#prune_prefix_symlinks_and_directories ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 |
# File 'cleanup.rb', line 989 def prune_prefix_symlinks_and_directories ObserverPathnameExtension.reset_counts! dirs = [] children_count = {} Keg.must_exist_subdirectories.each do |dir| next unless dir.directory? dir.find do |path| path.extend(ObserverPathnameExtension) if path.symlink? unless resolved_path_exists?(path) if path.to_s.match?(Keg::INFOFILE_RX) && !dry_run? Utils::Path.uninstall_info(path, verbose: ObserverPathnameExtension.verbose?) end if dry_run? puts "Would remove (broken link): #{path}" children_count[path.dirname] -= 1 if children_count.key?(path.dirname) else path.unlink end end elsif path.directory? && Keg.must_exist_subdirectories.exclude?(path) dirs << path children_count[path] = path.children.length if dry_run? end end end dirs.reverse_each do |d| if !dry_run? rmdir_if_possible(d) elsif children_count[d].zero? puts "Would remove (empty directory): #{d}" children_count[d.dirname] -= 1 if children_count.key?(d.dirname) end end require "cask/caskroom" if Cask::Caskroom.path.directory? Cask::Caskroom.path.each_child do |path| path.extend(ObserverPathnameExtension) next if !path.symlink? || resolved_path_exists?(path) if dry_run? puts "Would remove (broken link): #{path}" else path.unlink end end end return if dry_run? return if ObserverPathnameExtension.total.zero? n, d = ObserverPathnameExtension.counts print "Pruned #{n} symbolic links " print "and #{d} directories " if d.positive? puts "from #{HOMEBREW_PREFIX}" end |
#rm_ds_store(dirs = nil) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 |
# File 'cleanup.rb', line 922 def rm_ds_store(dirs = nil) return if dry_run? dirs ||= Keg.must_exist_directories + [ HOMEBREW_PREFIX/"Caskroom", ] dirs.select(&:directory?) .flat_map { |d| Pathname.glob("#{d}/**/.DS_Store") } .each do |dir| dir.unlink rescue Errno::EACCES # don't care if we can't delete a .DS_Store nil end end |
#scrub? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
313 |
# File 'cleanup.rb', line 313 def scrub? = @scrub |
#unremovable_kegs ⇒ Array<Keg>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
531 532 533 |
# File 'cleanup.rb', line 531 def unremovable_kegs @unremovable_kegs ||= T.let([], T.nilable(T::Array[Keg])) end |
#use_system_ruby? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
881 882 883 |
# File 'cleanup.rb', line 881 def use_system_ruby? false end |