Skip to content

Commit 697c3c9

Browse files
committed
Clean up unused checkouts on dev-desktops
User might checkout repositories on the dev-desktops, work on them for a while, and then move on to other projects. The unused checkouts, in particular their `build` or `target` directories, can use up a lot of space that we want to reclaim periodically. A script has been created that finds all cache directories, checks recursively if any file in their root directory has been modified within a given time frame, and if not deletes the cache directory.
1 parent aff57e6 commit 697c3c9

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bash
2+
3+
# Clean up unused checkouts
4+
#
5+
# This script is used to find old checkouts that are no longer in use. Given the
6+
# size of the build directory, regularly cleaning them up can save significant
7+
# amounts of disk space.
8+
9+
# Enable strict mode for Bash
10+
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
11+
set -euo pipefail
12+
IFS=$'\n\t'
13+
14+
# Print directories and their size instead of deleting them
15+
dry_run=false
16+
17+
# Default to search for checkouts older than 60 days
18+
time="60"
19+
20+
while [[ $# -gt 0 ]]; do
21+
case $1 in
22+
--dry-run)
23+
dry_run=true
24+
shift # past argument
25+
;;
26+
-t|--time)
27+
time="${2}"
28+
shift # past argument
29+
shift # past value
30+
;;
31+
-*)
32+
echo "Unknown option $1"
33+
exit 1
34+
;;
35+
esac
36+
done
37+
38+
# Find all build or target directories created by users
39+
#
40+
# This command combines (`-o`) two different conditions to find all build and
41+
# target directories that users have created. Within each home directory, we
42+
# recursively look for directories that either have a file named `x.py` and a
43+
# directory named `build`, or a file named `Cargo.toml` and a directory named
44+
# `target`.
45+
all_cache_directories=$(find /home -type d \( -name build -execdir test -f "x.py" \; -o -name target -execdir test -f "Cargo.toml" \; \) -print | sort | uniq)
46+
47+
# For each checkout, we want to determine if the user has been working on it
48+
# within the `$time` number of days.
49+
unused_cache_directories=$(for directory in $all_cache_directories; do
50+
project=$(dirname "${directory}")
51+
52+
# Find all directories with files that have been modified less than $time days ago
53+
modified=$(find "${project}" -type f -mtime -"${time}" -printf '%h\n' | xargs -r dirname | sort | uniq)
54+
55+
# If no files have been modified in the last 90 days, then the project is
56+
# considered old.
57+
if [[ -z "${modified}" ]]; then
58+
echo "${directory}"
59+
fi
60+
done)
61+
62+
# Delete the build directories in the unused checkouts
63+
for directory in $unused_cache_directories; do
64+
if [[ "${dry_run}" == true ]]; then
65+
du -sh "${directory}"
66+
else
67+
echo "Deleting ${directory}"
68+
rm -rf "${directory}"
69+
fi
70+
done

0 commit comments

Comments
 (0)