Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save akunzai/1f4ff6e9859f4134eaa1ffcd7bb144af to your computer and use it in GitHub Desktop.
Save akunzai/1f4ff6e9859f4134eaa1ffcd7bb144af to your computer and use it in GitHub Desktop.
VSCode unused workspaceStorage cleanup
#!/bin/bash
# https://code.visualstudio.com/docs/getstarted/settings#_settings-file-locations
case "$(uname -sr)" in
Darwin*)
VSCODE_HOME="$HOME/Library/Application Support/Code/User"
;;
Linux*WSL*)
# https://github.com/wslutilities/wslu
if hash wslvar 2>/dev/null; then
VSCODE_HOME="$(wslpath "$(wslvar APPDATA)")/Code/User"
else
VSCODE_HOME="/mnt/c/Users/$USER/AppData/Roaming/Code/User"
fi
;;
Linux*)
VSCODE_HOME="$HOME/.config/Code/User"
;;
*)
echo "ERROR: Unsupported OS"
exit 1
;;
esac
OIFS="$IFS"
IFS=$'\n'
for wsJson in $(find $VSCODE_HOME/workspaceStorage -maxdepth 2 -type f -name workspace.json); do
folder="$(python3 -c "import sys, json; print(json.load(open(sys.argv[1], 'r'))['folder'])" "$wsJson")"
if [[ $folder == file://* ]]; then
if hash wslpath 2>/dev/null; then
folder="$(wslpath "$(echo "$folder" | sed 's#^file:///##;s/+/ /g;s/%\(..\)/\\x\1/g;s/\\x3A/:/g')" 2>/dev/null)"
else
folder="$(echo "$folder" | sed 's#^file://##;s/+/ /g;s/%\(..\)/\\x\1/g;')"
fi
if [ -n "$folder" ] && [ ! -e "$folder" ]; then
wsPath=$(dirname $wsJson)
echo "Removing workspace $(basename $wsPath) for deleted folder $folder of size $(du -sh $wsPath | cut -f1)"
rm -rf "$wsPath"
fi
fi
done
IFS="$OIFS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment