11#! /bin/bash
2+ set -euo pipefail
23
34# Free disk space on Linux GitHub action runners
45# Script inspired by https://github.com/jlumbroso/free-disk-space
@@ -54,17 +55,6 @@ printDF() {
5455 printSeparationLine " ="
5556}
5657
57- removeDir () {
58- dir=${1}
59-
60- local before
61- before=$( getAvailableSpace)
62-
63- sudo rm -rf " $dir " || true
64-
65- printSavedSpace " $before " " $dir "
66- }
67-
6858execAndMeasureSpaceChange () {
6959 local operation=${1} # Function to execute
7060 local title=${2}
@@ -79,39 +69,103 @@ execAndMeasureSpaceChange() {
7969# Remove large packages
8070# REF: https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
8171cleanPackages () {
82- sudo apt-get -qq remove -y --fix-missing \
83- ' ^aspnetcore-.*' \
84- ' ^dotnet-.*' \
85- ' ^llvm-.*' \
86- ' php.*' \
87- ' ^mongodb-.*' \
88- ' ^mysql-.*' \
89- ' azure-cli' \
90- ' google-chrome-stable' \
91- ' firefox' \
92- ' powershell' \
93- ' mono-devel' \
94- ' libgl1-mesa-dri' \
95- ' google-cloud-sdk' \
96- ' google-cloud-cli'
72+ # Prevent npm from being removed.
73+ # This command unfortunately also prevents npm from being updated,
74+ # but it should be fine because we don't run `apt upgrade`.
75+ sudo apt-mark hold npm
76+
77+ sudo apt-get -qq purge -y --autoremove --fix-missing \
78+ ' ^aspnetcore-.*' \
79+ ' ^dotnet-.*' \
80+ ' ^java-*' \
81+ ' ^libllvm.*' \
82+ ' ^llvm.*' \
83+ ' ^mongodb-.*' \
84+ ' ^mysql-.*' \
85+ ' ^r-base.*' \
86+ ' ^vim.*' \
87+ ' azure-cli' \
88+ ' cpp-11' \
89+ ' firefox' \
90+ ' gcc-10' \
91+ ' gcc-11' \
92+ ' gcc-12' \
93+ ' gcc-9' \
94+ ' gcc' \
95+ ' google-chrome-stable' \
96+ ' google-cloud-cli' \
97+ ' google-cloud-sdk' \
98+ ' groff-base' \
99+ ' groff' \
100+ ' kubectl' \
101+ ' libgl1-mesa-dri' \
102+ ' libicu-dev' \
103+ ' mercurial-common' \
104+ ' microsoft-edge-stable' \
105+ ' mono-devel' \
106+ ' mono-llvm-tools' \
107+ ' php.*' \
108+ ' podman' \
109+ ' powershell' \
110+ ' python-babel-localedata' \
111+ ' python3-breezy' \
112+ ' skopeo' \
113+ ' snapd' \
114+ ' tmux'
97115
98116 sudo apt-get autoremove -y || echo " ::warning::The command [sudo apt-get autoremove -y] failed"
99117 sudo apt-get clean || echo " ::warning::The command [sudo apt-get clean] failed failed"
118+
119+ echo " => Installed packages sorted by size:"
120+ # sort always fails because `head` stops reading stdin
121+ dpkg-query -W --showformat=' ${Installed-Size} ${Package}\n' | \
122+ sort -nr 2> /dev/null | head -200 || true
100123}
101124
102125# Remove Docker images
103126cleanDocker () {
104- echo " Removing the following docker images:"
127+ echo " => Removing the following docker images:"
105128 sudo docker image ls
106- echo " Removing docker images..."
129+ echo " => Removing docker images..."
107130 sudo docker image prune --all --force || true
108131}
109132
110- # Remove Swap storage
111- cleanSwap () {
112- sudo swapoff -a || true
113- sudo rm -rf /mnt/swapfile || true
114- free -h
133+ removeAllSnaps () {
134+ # This won't remove the snaps `core` and `snapd`
135+ sudo snap remove $( snap list | awk ' !/^Name|^core|^snapd/ {print $1}' )
136+ }
137+
138+ removeUnusedDirectories () {
139+ local dirs_to_remove=(
140+ " /usr/lib/heroku/"
141+ " /usr/local/lib/android"
142+ " /usr/local/share/chromium"
143+ " /usr/local/share/powershell"
144+ " /usr/share/az_" *
145+ " /usr/local/share/cmake-" *
146+ " /usr/share/dotnet"
147+ " /usr/share/icons/"
148+ " /usr/share/miniconda/"
149+ " /usr/share/swift"
150+
151+ # Environemnt variable set by GitHub Actions
152+ " $AGENT_TOOLSDIRECTORY "
153+
154+ # Haskell runtime
155+ " /opt/ghc"
156+ " /usr/local/.ghcup"
157+ )
158+ local before
159+
160+ for dir in " ${dirs_to_remove[@]} " ; do
161+ before=$( getAvailableSpace)
162+ sudo rm -rf " $dir " || true
163+ printSavedSpace " $before " " Removed $dir "
164+ done
165+
166+ echo " => largest directories:"
167+ # sort always fails because `head` stops reading stdin
168+ sudo du --max-depth=7 /* -h | sort -nr 2> /dev/null | head -1000 || true
115169}
116170
117171# Display initial disk space stats
@@ -121,16 +175,11 @@ AVAILABLE_INITIAL=$(getAvailableSpace)
121175printDF " BEFORE CLEAN-UP:"
122176echo " "
123177
124- removeDir /usr/local/lib/android
125- removeDir /usr/share/dotnet
126-
127- # Haskell runtime
128- removeDir /opt/ghc
129- removeDir /usr/local/.ghcup
130-
131- execAndMeasureSpaceChange cleanPackages " Large misc. packages"
178+ execAndMeasureSpaceChange removeAllSnaps " Snaps"
179+ execAndMeasureSpaceChange cleanPackages " Unused packages"
132180execAndMeasureSpaceChange cleanDocker " Docker images"
133- execAndMeasureSpaceChange cleanSwap " Swap storage"
181+
182+ removeUnusedDirectories
134183
135184# Output saved space statistic
136185echo " "
0 commit comments