Skip to content

Instantly share code, notes, and snippets.

@jhochwald
Created December 29, 2025 18:25
Show Gist options
  • Select an option

  • Save jhochwald/daeb705c4c2c89fe592ca4ef027ac7d3 to your computer and use it in GitHub Desktop.

Select an option

Save jhochwald/daeb705c4c2c89fe592ca4ef027ac7d3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#set -x # Enable debugging
#set -euo pipefail # Strict error handling
IFS=$'\n\t' # Set IFS to handle spaces in filenames correctly
# Script to clear Microsoft Teams cache and restart the application on macOS
# The MSTeamsAudioDevice driver is removed to fix audio issues in Microsoft Teams
# Just in case!
if [ "$(uname -s)" != "Darwin" ]; then
error "This script runs only on macOS systems."
exit 2
fi
TEAMS_APP_NAME="Microsoft Teams"
# Close Microsoft Teams if it's running
if pgrep -f "$TEAMS_APP_NAME" >/dev/null 2>&1; then
osascript -e "tell application \"${TEAMS_APP_NAME}\" to quit" >/dev/null 2>&1 || true
sleep 2
fi
# Kill all for the Microsoft Teams processes to ensure complete closure
if pgrep -f "$TEAMS_APP_NAME" >/dev/null 2>&1; then
echo "Forcing kill for $TEAMS_APP_NAME"
pkill -f "$TEAMS_APP_NAME" >/dev/null 2>&1 || true
sleep 1
fi
# Define directories to clear
declare -a PATHS_TO_HANDLE=(
"$HOME/Library/Containers/com.microsoft.teams2/Data/Library/Application\ Support/Microsoft/MSTeams/"
"$HOME/Library/Group\ Containers/UBF8T346G9.com.microsoft.teams/"
"$HOME/Library/Containers/com.microsoft.teams2/com.microsoft.teams2.notificationcenter/"
"$HOME/Library/Containers/com.microsoft.teams2/com.microsoft.teams2.respawn/"
"$HOME/Library/Containers/com.microsoft.teams2/com.microsoft.teams2.widgetextension/"
"$HOME/Library/Containers/com.microsoft.teams2/Data/Library/Caches/com.microsoft.teams2/"
)
# Do the clearing
for TARGET_PATH in "${PATHS_TO_HANDLE[@]}"; do
if [ -d "$TARGET_PATH" ]; then
echo "Clearing contents of: $TARGET_PATH"
rm -rfv "${TARGET_PATH:?}/"* || echo "Failed to clear contents of: $TARGET_PATH"
else
echo "Directory does not exist, skipping: $TARGET_PATH"
fi
done
# Remove the MSTeamsAudioDevice.driver if it exists
if [ -d "/Library/Audio/Plug-Ins/HAL/MSTeamsAudioDevice.driver" ]; then
sudo rm -rfv "/Library/Audio/Plug-Ins/HAL/MSTeamsAudioDevice.driver" || echo "Failed to remove driver"
# Kill all for the Coreaudio process to reset audio services (twice for reliability)
if pgrep -f "$pattern" >/dev/null 2>&1; then
echo "Forcing kill for pattern: $pattern"
sudo pkill -f "$pattern" || true
fi
sleep 1
if pgrep -f "$pattern" >/dev/null 2>&1; then
iechonfo "Forcing kill for pattern: $pattern"
sudo pkill -f "$pattern" || true
fi
sleep 1
if sudo launchctl kickstart -k system/com.apple.audio.coreaudiod >/dev/null 2>&1; then
echo "Used launchctl kickstart to restart coreaudiod"
else
echo "launchctl method failed, falling back to killall"
sudo killall coreaudiod || true
fi
sleep 2
if pgrep -x coreaudiod >/dev/null 2>&1; then
echo "coreaudiod running again after restart"
else
echo "coreaudiod not running after restart attempt"
fi
fi
# Restart Microsoft Teams (Take longer time to launch due to cache clearing)
open -a "$TEAMS_APP_NAME" >/dev/null 2>&1 || warn "Failed to open $TEAMS_APP_NAME with open -a"
sleep 5
# Verify if Teams is running
if pgrep -f "$TEAMS_APP_NAME" >/dev/null 2>&1; then
echo "$TEAMS_APP_NAME is running. If audio issues persist, reboot or re-login may be required."
else
echo "Failed to start $TEAMS_APP_NAME."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment