Spotify ad blocker for macOS, Linux, and Windows.
A Bash script, with a PowerShell version for Windows, that blocks ad domains through your system's hosts file.
- Blocks a short list of known ad domains through the
hostsfile - Adds ad and promo entries to Spotify's
prefsfile when it exists and Spotify is closed - Backs up the
hostsfile before every change and restores the newest backup in one command - Clears the Spotify cache when old ads persist
- Installs as a
spotblockcommand with onecurlor PowerShell line - Needs only Bash and standard system tools, or Windows PowerShell 5.1 or later on Windows
Important
Blocking is partial. Most Spotify audio ads are served from the same API hosts that deliver music, and blocking those breaks playback. SpotBlock only blocks separate ad hosts, so some ads will still play.
| Platform | Requirement |
|---|---|
| macOS | None |
| Linux | None |
| Windows (PowerShell) | None. Run PowerShell as Administrator. |
| Windows (Git Bash) | Git Bash, run as Administrator |
On macOS, Linux, or Git Bash:
curl -fsSL https://raw.githubusercontent.com/kacigaya/spotblock/main/spotblock.sh | bashOn Windows, in PowerShell run as Administrator:
irm https://raw.githubusercontent.com/kacigaya/spotblock/main/spotblock.ps1 | iexBoth install SpotBlock as a spotblock command and run block. The PowerShell version installs to C:\Program Files\SpotBlock and adds its bin folder to the system Path. Open a new terminal to use spotblock.
To read the script before running it:
curl -fsSLO https://raw.githubusercontent.com/kacigaya/spotblock/main/spotblock.sh
less spotblock.sh
bash spotblock.sh installirm https://raw.githubusercontent.com/kacigaya/spotblock/main/spotblock.ps1 -OutFile spotblock.ps1
notepad spotblock.ps1
powershell -ExecutionPolicy Bypass -File .\spotblock.ps1 installgit clone https://github.com/kacigaya/spotblock.git
cd spotblock
chmod +x spotblock.sh
./spotblock.sh installThe Bash install downloads the script again from main instead of copying your local file. To run exactly the file you read without installing it, use sudo ./spotblock.sh <command> on macOS and Linux, or ./spotblock.sh <command> in Git Bash. The PowerShell install copies the local file when run from one.
| Variable | Default | Effect |
|---|---|---|
SPOTBLOCK_INSTALL_DIR |
/usr/local/bin, or C:\Program Files\SpotBlock for PowerShell |
Where the spotblock command is installed. In Git Bash, falls back to ~/bin when /usr/local/bin is not writable. In PowerShell, a folder you can write without Administrator rights goes on the user Path. |
SPOTBLOCK_RUN_AFTER_INSTALL |
1 |
Set to 0 to install without running the blocker. |
In PowerShell, set a variable with $env:SPOTBLOCK_RUN_AFTER_INSTALL = '0' before installing.
On macOS and Linux, run sudo spotblock <command>. On Windows, open PowerShell or Git Bash as Administrator and run spotblock <command>.
| Command | Description |
|---|---|
block |
Add the ad domains to hosts and the entries to prefs |
restore |
Copy the newest hosts backup back over hosts |
status |
Show whether Spotify is running and whether the SpotBlock header is in hosts |
clear-cache |
Delete the contents of the Spotify cache directory |
Sample output from a test run with a fake hosts file:
$ sudo spotblock block
Applying advanced blocking configurations...
Creating backup of hosts file at /home/you/.spotify_adblock_backups/hosts_backup_20261002_143005
Blocking: pagead2.googlesyndication.com
Blocking: gads.pubmatic.com
Blocking: securepubads.g.doubleclick.net
Blocking: spotify-heads-ak.akamaized.net
Blocking: heads-fab.spotify.com
Blocking: heads4-ak.spotify.com
Blocking: heads4-fa.spotify.com
Blocking: heads-cf.spotify.com
Spotify ad blocking has been applied.
Please restart Spotify for changes to take effect.
Backup saved at: /home/you/.spotify_adblock_backups/hosts_backup_20261002_143005
$ sudo spotblock status
Spotify is not running.
Ad blocking is currently active.
Number of blocked domains: 5block first checks that the hosts file is writable and stops without changing anything if it isn't. It then saves a timestamped copy of hosts to $HOME/.spotify_adblock_backups/ and appends a # Spotify Ad Blocking header followed by the ad domains, each pointed to 127.0.0.1. Domains already in the file are skipped. The PowerShell version does not add the header when every domain is already there.
$HOME is the home directory as seen by the script. Under sudo it can be root's home instead of yours, depending on sudo settings. Most Linux systems use /root. The PowerShell version uses %USERPROFILE%\.spotify_adblock_backups.
| Platform | Hosts file |
|---|---|
| macOS, Linux | /etc/hosts |
| Windows | %SystemRoot%\System32\drivers\etc\hosts, usually C:\Windows\System32\drivers\etc\hosts |
The list holds three ad network hosts (googlesyndication.com, pubmatic.com, doubleclick.net) and five Spotify heads ad hosts. A hosts file matches exact hostnames only, so wildcards are not supported.
If a prefs file exists and Spotify is closed, block also appends entries such as ui.show_ads=false and ui.promo_enabled=false. These keys come from community lists, not from Spotify documentation, and current clients may ignore them. The list also turns off autoplay, gapless playback, and volume normalization. The prefs file is not backed up.
| Platform | Prefs file |
|---|---|
| macOS | ~/Library/Application Support/Spotify/prefs |
| Linux | ~/.config/spotify/prefs, or ~/.var/app/com.spotify.Client/config/spotify/prefs for Flatpak when the first is missing |
| Windows | %APPDATA%\Spotify\prefs |
clear-cache empties the Spotify cache directory and keeps the directory itself.
| Platform | Cache directory |
|---|---|
| macOS | ~/Library/Application Support/Spotify/PersistentCache |
| Linux | ~/.cache/spotify and ~/.var/app/com.spotify.Client/cache/spotify (Flatpak) |
| Windows | %APPDATA%\Spotify\Data |
On Linux, ~ in these paths is the home directory of the user who ran sudo, not /root.
Note
Restart Spotify after blocking so the hosts changes take effect. If ads persist, run clear-cache and restart again.
Warning
Spotify may change its ad domains at any time, so the blocklist can need updates and results may vary.
prefschanges don't stick. Spotify rewritesprefswhen it exits, so entries added while it runs are lost.blockskipsprefswhile Spotify is running and says so. Quit Spotify fully, including from the tray or menu bar, then runblockagain.blockfails on Windows withcannot write. Editinghostsneeds Administrator rights. Close PowerShell or Git Bash, reopen it with Run as administrator, and runblockagain.running scripts is disabled on this system. Windows blocks.ps1files by default. Use the installedspotblockcommand, which runs the script with-ExecutionPolicy Bypass, or runpowershell -ExecutionPolicy Bypass -File .\spotblock.ps1 <command>.spotblockis not found after installing on Windows. Open a new terminal so it picks up the updatedPath.- Microsoft Store version of Spotify. It keeps
prefsunder%LOCALAPPDATA%\Packages\SpotifyAB.SpotifyMusic_*\LocalState\Spotify\prefs, which SpotBlock does not handle.hostsblocking still applies. - Snap version of Spotify on Linux. SpotBlock does not handle the Snap
prefsor cache paths. A customXDG_CONFIG_HOMEis also ignored. statussays active but nothing changed.statusonly checks for the# Spotify Ad Blockingheader inhosts. It does not check each domain or whether Spotify uses them. In the Bash version, the domain count is the number of lines containingspotify, so it shows 5 when all 8 domains are blocked. The PowerShell version counts the listed domains found inhosts.- No
Spotify.exe.bakfile. SpotBlock never patchesSpotify.exe, so no.bakfile is created.
-
Restore
hosts:sudo spotblock restore # on Windows, run without sudo in an Administrator Git Bash -
Remove the command. Use
command -v spotblockto find it if you setSPOTBLOCK_INSTALL_DIR.sudo rm /usr/local/bin/spotblock # macOS, Linux rm -f /usr/local/bin/spotblock ~/bin/spotblock # Windows
-
Optionally, delete the backups. On macOS and Linux, run this through
sudoso$HOMEmatches the one used byblock. On Windows, runrm -rf ~/.spotify_adblock_backups.sudo sh -c 'rm -rf "$HOME/.spotify_adblock_backups"'
Run these in PowerShell as Administrator:
spotblock restore
Remove-Item -Recurse "$env:ProgramFiles\SpotBlock"
Remove-Item -Recurse "$HOME\.spotify_adblock_backups" # optionalThen remove C:\Program Files\SpotBlock\bin from the system Path under System Properties > Environment Variables.
restore copies the newest backup, which was taken before the last block run. If you ran block more than once, that backup already contains SpotBlock entries. Delete the lines from the first # Spotify Ad Blocking header to the end of hosts, or copy the oldest backup instead. restore does not touch prefs. Remove the added entries by hand while Spotify is closed.
- To suggest a domain, open a pull request that adds it to
ad_domainsinspotblock.shand$AdDomainsinspotblock.ps1, and explain how you confirmed it serves ads without breaking playback. - Ask questions in Discussions Q&A.
- Report bugs in Issues.
This script is provided for educational purposes only. Modifying system files may have unintended consequences. Use it at your own risk.
Blocking ads may violate Spotify's Terms of Use. You are responsible for how you use this script.