build icon indicating copy to clipboard operation
build copied to clipboard

Clean up macOS cache and more

Open UlisesGascon opened this issue 2 years ago • 10 comments
trafficstars

As commented in https://github.com/nodejs/build/issues/2917#issuecomment-1320930987. We need a basic bash script to clean up the mac, I created a fork from paulaime/CleanUpMac:

cleaner.sh
    # Maintenance script for macOS
    # Custom script (forked from https://github.com/paulaime/CleanUpMac)
    #!/usr/bin/env bash

    # Ask for the administrator password upfront
    if [ "$EUID" -ne 0  ]; then
        echo "Please run as root"
        exit
    fi


    echo 'Empty the Trash on all mounted volumes and the main HDD…'
    sudo rm -rfv /Volumes/*/.Trashes &>/dev/null
    sudo rm -rfv ~/.Trash &>/dev/null

    echo 'Clear System Log Files…'
    sudo rm -rfv /private/var/log/asl/*.asl &>/dev/null
    sudo rm -rfv /Library/Logs/DiagnosticReports/* &>/dev/null
    sudo rm -rfv /Library/Logs/Adobe/* &>/dev/null
    rm -rfv ~/Library/Containers/com.apple.mail/Data/Library/Logs/Mail/* &>/dev/null
    rm -rfv ~/Library/Logs/CoreSimulator/* &>/dev/null

    echo 'Clear Adobe Cache Files…'
    sudo rm -rfv ~/Library/Application\ Support/Adobe/Common/Media\ Cache\ Files/* &>/dev/null

    echo 'Cleanup iOS Applications…'
    rm -rfv ~/Music/iTunes/iTunes\ Media/Mobile\ Applications/* &>/dev/null

    echo 'Remove iOS Device Backups…'
    rm -rfv ~/Library/Application\ Support/MobileSync/Backup/* &>/dev/null

    echo 'Cleanup XCode Derived Data and Archives…'
    rm -rfv ~/Library/Developer/Xcode/DerivedData/* &>/dev/null
    rm -rfv ~/Library/Developer/Xcode/Archives/* &>/dev/null

    echo 'Cleanup Homebrew Cache…'
    brew cleanup --force -s &>/dev/null
    brew cask cleanup &>/dev/null
    rm -rfv /Library/Caches/Homebrew/* &>/dev/null
    brew tap --repair &>/dev/null

    echo 'Cleanup MacOS Application caches'
    rm -rfv /System/Library/Caches/*

    echo 'Cleanup any old versions of gems…'
    gem cleanup &>/dev/null

    echo 'Purge inactive memory…'
    sudo purge

Logs

Here are some logs from the execution made in test-orka-macos11-x64-1

Before the script
```
administrator@test-orka-macos11-x64-1 ~ % uptime
20:36  up 3 days,  6:08, 1 user, load averages: 1,78 1,90 1,88
administrator@test-orka-macos11-x64-1 ~ % df -h
Filesystem       Size   Used  Avail Capacity iused     ifree %iused  Mounted on
/dev/disk2s5s1   90Gi   14Gi   19Gi    44%  553788 941116212    0%   /
devfs           189Ki  189Ki    0Bi   100%     653         0  100%   /dev
/dev/disk2s4     90Gi  515Mi   19Gi     3%       2 941669998    0%   /System/Volumes/VM
/dev/disk2s2     90Gi  305Mi   19Gi     2%    1038 941668962    0%   /System/Volumes/Preboot
/dev/disk2s6     90Gi  504Ki   19Gi     1%      17 941669983    0%   /System/Volumes/Update
/dev/disk2s1     90Gi   55Gi   19Gi    75%  330544 941339456    0%   /System/Volumes/Data
map auto_home     0Bi    0Bi    0Bi   100%       0         0  100%   /System/Volumes/Data/home
administrator@test-orka-macos11-x64-1 ~ % sudo find / -size +200M -exec du -hs {} \;
395M	/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/objects/pack/pack-3a0743b6a6abca2de79dc7381cc965175cab5bb2.pack
86M	/Library/Developer/CommandLineTools/usr/bin/swift-frontend
82M	/Library/Developer/CommandLineTools/usr/lib/swift/macosx/lib_InternalSwiftScan.dylib
88M	/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/LLDB
306M	/System/Library/SpeechBase/Voices/AlexCompact.SpeechVoice/Contents/Resources/PCMWave
897M	/System/Library/dyld/dyld_shared_cache_arm64e
1,0G	/System/Library/dyld/aot_shared_cache
959M	/System/Library/dyld/dyld_shared_cache_x86_64h
960M	/System/Library/dyld/dyld_shared_cache_x86_64
167M	/System/Library/KernelCollections/SystemKernelExtensions.kc
594M	/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/BaseSystem.dmg
911M	/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/022-12376-025.dmg
186M	/System/Library/Fonts/Apple Color Emoji.ttc
36G	/System/Library/Caches/com.apple.coresymbolicationd/data
512M	/System/Volumes/VM/swapfile0
395M	/System/Volumes/Data/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/objects/pack/pack-3a0743b6a6abca2de79dc7381cc965175cab5bb2.pack
86M	/System/Volumes/Data/Library/Developer/CommandLineTools/usr/bin/swift-frontend
82M	/System/Volumes/Data/Library/Developer/CommandLineTools/usr/lib/swift/macosx/lib_InternalSwiftScan.dylib
88M	/System/Volumes/Data/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/LLDB
594M	/System/Volumes/Data/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/BaseSystem.dmg
911M	/System/Volumes/Data/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/022-12376-025.dmg
36G	/System/Volumes/Data/System/Library/Caches/com.apple.coresymbolicationd/data
911M	/System/Volumes/Data/Users/iojs/build/workspace/node-test-commit-osx/nodes/osx11-x64/.git/objects/pack/pack-131ed30e160c47cd7a9e02d1fb24e38c3d071ee4.pack
911M	/Users/iojs/build/workspace/node-test-commit-osx/nodes/osx11-x64/.git/objects/pack/pack-131ed30e160c47cd7a9e02d1fb24e38c3d071ee4.pack
```
Preparation and Execution
```
administrator@test-orka-macos11-x64-1 ~ % touch cleaner.sh
administrator@test-orka-macos11-x64-1 ~ % nano cleaner.sh
# Paste the file content
administrator@test-orka-macos11-x64-1 ~ % chmod 777 cleaner.sh
administrator@test-orka-macos11-x64-1 ~ % sudo ./cleaner.sh
Empty the Trash on all mounted volumes and the main HDD…
Clear System Log Files…
Clear Adobe Cache Files…
Cleanup iOS Applications…
Remove iOS Device Backups…
Cleanup XCode Derived Data and Archives…
Cleanup Homebrew Cache…
Cleanup MacOS Application caches
/System/Library/Caches/MobileActivation/data_ark.plist
/System/Library/Caches/MobileActivation
/System/Library/Caches/OnDemandResources/AssetPacks
/System/Library/Caches/OnDemandResources
/System/Library/Caches/com.apple.CVMS
/System/Library/Caches/com.apple.Components2.SystemCache.Components
/System/Library/Caches/com.apple.IntlDataCache.le
/System/Library/Caches/com.apple.IntlDataCache.le.kbdx
/System/Library/Caches/com.apple.SystemMigrationUserSizeCache
/System/Library/Caches/com.apple.apfsd.stats.plist
/System/Library/Caches/com.apple.coresymbolicationd/data
/System/Library/Caches/com.apple.coresymbolicationd
/System/Library/Caches/com.apple.factorydata
/System/Library/Caches/com.apple.kernelcaches
rm: /System/Library/Caches/com.apple.kext.caches/Directories/System/Library/Extensions: Operation not permitted
rm: /System/Library/Caches/com.apple.kext.caches/Directories/System/Library: Operation not permitted
rm: /System/Library/Caches/com.apple.kext.caches/Directories/System: Operation not permitted
rm: /System/Library/Caches/com.apple.kext.caches/Directories: Operation not permitted
rm: /System/Library/Caches/com.apple.kext.caches/Startup: Operation not permitted
rm: /System/Library/Caches/com.apple.kext.caches: Operation not permitted
/System/Library/Caches/com.apple.preferencepanes.systemcache
Cleanup any old versions of gems…
Purge inactive memory…
```
After the execution
```
administrator@test-orka-macos11-x64-1 ~ % uptime
20:42  up 3 days,  6:14, 1 user, load averages: 2,91 2,12 1,94
administrator@test-orka-macos11-x64-1 ~ % df -h
Filesystem       Size   Used  Avail Capacity iused     ifree %iused  Mounted on
/dev/disk2s5s1   90Gi   14Gi   19Gi    44%  553788 941116212    0%   /
devfs           188Ki  188Ki    0Bi   100%     650         0  100%   /dev
/dev/disk2s4     90Gi  515Mi   19Gi     3%       2 941669998    0%   /System/Volumes/VM
/dev/disk2s2     90Gi  305Mi   19Gi     2%    1038 941668962    0%   /System/Volumes/Preboot
/dev/disk2s6     90Gi  504Ki   19Gi     1%      17 941669983    0%   /System/Volumes/Update
/dev/disk2s1     90Gi   55Gi   19Gi    75%  330421 941339579    0%   /System/Volumes/Data
map auto_home     0Bi    0Bi    0Bi   100%       0         0  100%   /System/Volumes/Data/home
administrator@test-orka-macos11-x64-1 ~ % sudo find / -size +200M -exec du -hs {} \;
395M    /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/objects/pack/pack-3a0743b6a6abca2de79dc7381cc965175cab5bb2.pack
86M    /Library/Developer/CommandLineTools/usr/bin/swift-frontend
82M    /Library/Developer/CommandLineTools/usr/lib/swift/macosx/lib_InternalSwiftScan.dylib
88M    /Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/LLDB
306M    /System/Library/SpeechBase/Voices/AlexCompact.SpeechVoice/Contents/Resources/PCMWave
897M    /System/Library/dyld/dyld_shared_cache_arm64e
1,0G    /System/Library/dyld/aot_shared_cache
959M    /System/Library/dyld/dyld_shared_cache_x86_64h
960M    /System/Library/dyld/dyld_shared_cache_x86_64
167M    /System/Library/KernelCollections/SystemKernelExtensions.kc
594M    /System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/BaseSystem.dmg
911M    /System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/022-12376-025.dmg
186M    /System/Library/Fonts/Apple Color Emoji.ttc
512M    /System/Volumes/VM/swapfile0
395M    /System/Volumes/Data/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/objects/pack/pack-3a0743b6a6abca2de79dc7381cc965175cab5bb2.pack
86M    /System/Volumes/Data/Library/Developer/CommandLineTools/usr/bin/swift-frontend
82M    /System/Volumes/Data/Library/Developer/CommandLineTools/usr/lib/swift/macosx/lib_InternalSwiftScan.dylib
88M    /System/Volumes/Data/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/LLDB
594M    /System/Volumes/Data/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/BaseSystem.dmg
911M    /System/Volumes/Data/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/022-12376-025.dmg
911M    /System/Volumes/Data/Users/iojs/build/workspace/node-test-commit-osx/nodes/osx11-x64/.git/objects/pack/pack-131ed30e160c47cd7a9e02d1fb24e38c3d071ee4.pack
911M    /Users/iojs/build/workspace/node-test-commit-osx/nodes/osx11-x64/.git/objects/pack/pack-131ed30e160c47cd7a9e02d1fb24e38c3d071ee4.pack
```
After Reboot!
```
administrator@test-orka-macos11-x64-1 ~ % uptime
20:47  up 56 secs, 1 user, load averages: 2,47 0,75 0,28
administrator@test-orka-macos11-x64-1 ~ % df -h
Filesystem       Size   Used  Avail Capacity iused     ifree %iused  Mounted on
/dev/disk2s5s1   90Gi   14Gi   55Gi    21%  553788 941116212    0%   /
devfs           188Ki  188Ki    0Bi   100%     650         0  100%   /dev
/dev/disk2s4     90Gi  1.0Mi   55Gi     1%       1 941669999    0%   /System/Volumes/VM
/dev/disk2s2     90Gi  305Mi   55Gi     1%    1038 941668962    0%   /System/Volumes/Preboot
/dev/disk2s6     90Gi  516Ki   55Gi     1%      17 941669983    0%   /System/Volumes/Update
/dev/disk2s1     90Gi   20Gi   55Gi    27%  330435 941339565    0%   /System/Volumes/Data
map auto_home     0Bi    0Bi    0Bi   100%       0         0  100%   /System/Volumes/Data/home
administrator@test-orka-macos11-x64-1 ~ % sudo find / -size +200M -exec du -hs {} \;
395M	/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/objects/pack/pack-3a0743b6a6abca2de79dc7381cc965175cab5bb2.pack
86M	/Library/Developer/CommandLineTools/usr/bin/swift-frontend
82M	/Library/Developer/CommandLineTools/usr/lib/swift/macosx/lib_InternalSwiftScan.dylib
88M	/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/LLDB
306M	/System/Library/SpeechBase/Voices/AlexCompact.SpeechVoice/Contents/Resources/PCMWave
897M	/System/Library/dyld/dyld_shared_cache_arm64e
1,0G	/System/Library/dyld/aot_shared_cache
959M	/System/Library/dyld/dyld_shared_cache_x86_64h
960M	/System/Library/dyld/dyld_shared_cache_x86_64
167M	/System/Library/KernelCollections/SystemKernelExtensions.kc
594M	/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/BaseSystem.dmg
911M	/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/022-12376-025.dmg
186M	/System/Library/Fonts/Apple Color Emoji.ttc
395M	/System/Volumes/Data/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/objects/pack/pack-3a0743b6a6abca2de79dc7381cc965175cab5bb2.pack
86M	/System/Volumes/Data/Library/Developer/CommandLineTools/usr/bin/swift-frontend
82M	/System/Volumes/Data/Library/Developer/CommandLineTools/usr/lib/swift/macosx/lib_InternalSwiftScan.dylib
88M	/System/Volumes/Data/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/LLDB
594M	/System/Volumes/Data/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/BaseSystem.dmg
911M	/System/Volumes/Data/System/Library/AssetsV2/com_apple_MobileAsset_MacSoftwareUpdate/c7a6cff95465966476cbebf21c6580581582eabc.asset/AssetData/Restore/022-12376-025.dmg
911M	/System/Volumes/Data/Users/iojs/build/workspace/node-test-commit-osx/nodes/osx11-x64/.git/objects/pack/pack-131ed30e160c47cd7a9e02d1fb24e38c3d071ee4.pack
911M	/Users/iojs/build/workspace/node-test-commit-osx/nodes/osx11-x64/.git/objects/pack/pack-131ed30e160c47cd7a9e02d1fb24e38c3d071ee4.pack
```

Conclusions

The script seems working, as we deleted big files like /System/Volumes/Data/System/Library/Caches/com.apple.coresymbolicationd/data with 36G and the machine seems stable 🤖 .

Not sure why, but running df -h without rebooting returned weird results (very similar to prior execution). In any case we will reboot the machine after manually deleting the cache files, so we can ignore this.

Diff: Before vs after Captura de Pantalla 2022-11-22 a las 6 17 21

Diff: Before vs after reboot Captura de Pantalla 2022-11-22 a las 6 18 06

Next steps

If we are happy with this approach we can start to work on a Jenkins job that will execute this on a cron base (once per week?) 🦾

UlisesGascon avatar Nov 22 '22 05:11 UlisesGascon