Skip to content

Instantly share code, notes, and snippets.

@gerardroche
Last active June 3, 2024 19:16
Show Gist options
  • Save gerardroche/6e46cbdf8da19a39f9da to your computer and use it in GitHub Desktop.
Save gerardroche/6e46cbdf8da19a39f9da to your computer and use it in GitHub Desktop.
Clean Sublime Text caches and optionally clean out any sessions
#!/bin/sh
set -e
unset CDPATH
unset IFS
show_usage() {
cat <<USAGE
Usage: [PROJECTS_PATH=<PATH>] $(basename "$0") [--exclude-sessions] [--exclude-workspaces]
Cleaning will *only* remove files generated by Sublime Text at runtime like
caches, indexes, workspaces, sessions, etc. It will not remove installed
packages, configuration files, etc.
By default workspaces in the following locations are removed:
1. ./*.sublime-workspace
2. ./*/*.sublime-workspace
To specify another location to find workspaces set the PROJECTS_PATH
environment variable.
Example:
$ PROJECTS_PATH=~/projects sublime-clean
The above will remove workspaces from the following locations:
1. ./*.sublime-workspace
2. ./*/*.sublime-workspace
3. ~/projects/*.sublime-workspace
4. ~/projects/*/*.sublime-workspace
5. ~/projects/*/*/*.sublime-workspace
6. ~/projects/.{sublime,st}/*.sublime-workspace
7. ~/projects/.{sublime,st}-workspaces/*.sublime-workspace
8. ~/projects/.{sublime,st}-projects/*.sublime-workspace
USAGE
}
SUBLIME_CLEAN_INCLUDE_SESSIONS=y
SUBLIME_CLEAN_INCLUDE_WORKSPACES=y
while test "$#" != 0; do
case "$1" in
--help|-h) show_usage; exit 0 ;;
--exclude-sessions) SUBLIME_CLEAN_INCLUDE_SESSIONS= ;;
--exclude-workspaces) SUBLIME_CLEAN_INCLUDE_WORKSPACES= ;;
--) shift; break ;; # standard end of options list
-*) echo >&2 "$(basename "$0"): unknown option '$1'"; exit 1 ;;
*) echo >&2 "$(basename "$0"): unknown argument '$1'"; exit 1 ;;
esac
shift
done
if test -n "$(pgrep -f "(subl.*\/plugin_host [0-9]+$|^subl$)")"; then
echo >&2 "Please close Sublime Text and run script again"
exit 1
fi
echo "cleaning sublime text 2 installation..."
# ST2 Linux
rm -rfv ~/.config/sublime-text-2/Installed\ Packages/
rm -rfv ~/.config/sublime-text-2/Packages/
rm -rfv ~/.config/sublime-text-2/Pristine\ Packages/
# ST2 OSX
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 2/Installed\ Packages/
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 2/Pristine\ Packages/
if [ "$SUBLIME_CLEAN_INCLUDE_SESSIONS" = "y" ]; then
# ST2 Linux sessions
rm -fv ~/.config/sublime-text-2/Settings/*.sublime_session
# ST2 OSX sessions
rm -fv ~/Library/Application\ Support/Sublime\ Text\ 2/Settings/*.sublime_session
fi
rmdir_if_empty() {
if test -d "$1"; then
echo "rmdir_if_empty: removing empty directory, '$1'"
rmdir "$1" || true
fi
}
echo "cleaning sublime text 3 installation..."
# ST3 & ST4
rm -rfv ~/.config/sublime-text*/Backup/
rm -rfv ~/.config/sublime-text*/Cache/
rm -rfv ~/.config/sublime-text*/Index/
rm -rfv ~/.config/sublime-text*/Log/
rm -rfv ~/.config/sublime-text*/Packages/.logs/
rm -rfv ~/.config/sublime-text*/Packages/User/*.log*
rm -rfv ~/.config/sublime-text*/Packages/User/Color\ Highlighter/icons/
rm -rfv ~/.config/sublime-text*/Packages/User/Package\ Control.*-ca-bundle
rm -rfv ~/.config/sublime-text*/Packages/User/Package\ Control.cache/
rm -rfv ~/.config/sublime-text*/Packages/User/SublimeLinter/*.tmTheme
rm -rfv ~/.config/sublime-text*/Packages/User/UnitTesting/
rm -rfv ~/.config/sublime-text*/Settings/
rmdir_if_empty ~/.config/sublime-text-3/Lib/python33/
rmdir_if_empty ~/.config/sublime-text-3/Lib/python38/
rmdir_if_empty ~/.config/sublime-text-3/Lib/
rmdir_if_empty ~/.config/sublime-text-3/Packages/User/Color\ Highlighter/
rmdir_if_empty ~/.config/sublime-text-3/Packages/User/SublimeLinter/
rmdir_if_empty ~/.config/sublime-text/Lib/python33/
rmdir_if_empty ~/.config/sublime-text/Lib/python38/
rmdir_if_empty ~/.config/sublime-text/Lib/
rmdir_if_empty ~/.config/sublime-text/Packages/User/Color\ Highlighter/
rmdir_if_empty ~/.config/sublime-text/Packages/User/SublimeLinter/
# OSX ST3
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Backup/
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Cache/
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Index/
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/.logs/
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/Package\ Control.*-ca-bundle
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/*.log*
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/Color\ Highlighter/icons/
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/Package\ Control.cache/
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/SublimeLinter/*.tmTheme
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/UnitTesting/
rm -rfv ~/Library/Application\ Support/Sublime\ Text\ 3/Settings/
rmdir_if_empty ~/Library/Application\ Support/Sublime\ Text\ 3/Lib/python3.3/
rmdir_if_empty ~/Library/Application\ Support/Sublime\ Text\ 3/Lib/
rmdir_if_empty ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/Color\ Highligher/
rmdir_if_empty ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/SublimeLinter/
if [ "$SUBLIME_CLEAN_INCLUDE_SESSIONS" = "y" ]; then
echo "cleaning sublime text sessions..."
# ST3 Linux sessions
rm -fv ~/.config/sublime-text-3/Local/*.sublime_session
# ST4 Linux sessions
rm -fv ~/.config/sublime-text/Local/*.sublime_session
# ST3 OSX sessions
rm -fv ~/Library/Application\ Support/Sublime\ Text\ 3/Local/*.sublime_session
# NeoVintageous
rm -rfv ~/.config/sublime-text*/Local/nvinfo
fi
if [ "$SUBLIME_CLEAN_INCLUDE_WORKSPACES" = "y" ]; then
echo "cleaning sublime text workspaces..."
rm -fv ./*.sublime-workspace
rm -fv ./*/*.sublime-workspace
if [ -d "$PROJECTS_PATH" ]; then
rm -fv "$PROJECTS_PATH"/*.sublime-workspace
rm -fv "$PROJECTS_PATH"/.reload-package
rm -rfv "$PROJECTS_PATH"/.mypy_cache/
rm -rfv "$PROJECTS_PATH"/.rope_project/
rm -fv "$PROJECTS_PATH"/*/*.sublime-workspace
rm -fv "$PROJECTS_PATH"/*/.reload-package
rm -rfv "$PROJECTS_PATH"/*/.mypy_cache/
rm -rfv "$PROJECTS_PATH"/*/.rope_project/
rm -fv "$PROJECTS_PATH"/*/*/*.sublime-workspace
rm -fv "$PROJECTS_PATH"/*/*/.reload-package
rm -rfv "$PROJECTS_PATH"/*/*/.mypy_cache/
rm -rfv "$PROJECTS_PATH"/*/*/.rope_project/
rm -fv "$PROJECTS_PATH"/.sublime/*.sublime-workspace
rm -fv "$PROJECTS_PATH"/.sublime-workspaces/*.sublime-workspace
rm -fv "$PROJECTS_PATH"/.sublime-projects/*.sublime-workspace
rm -fv "$PROJECTS_PATH"/.st/*.sublime-workspace
rm -fv "$PROJECTS_PATH"/.st-workspaces/*.sublime-workspace
rm -fv "$PROJECTS_PATH"/.st-projects/*.sublime-workspace
fi
fi
echo "Done."
@OlivierBlanvillain
Copy link

rm -rfv ~/.config/sublime-text-3/Packages/.logs/		
rm -rfv ~/.config/sublime-text-3/Backup/		
rm -rfv ~/.config/sublime-text-3/Cache/		
rm -rfv ~/.config/sublime-text-3/Index/		
rm -rfv ~/.config/sublime-text-3/Settings/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment