User Agents

Web browsers and everyday web tech, explained

Disable Downloads in Headless Chrome: Step-by-Step Guide

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Disabling downloads in headless Chrome prevents automatic file downloads during automated browsing sessions, which is essential for maintaining script stability, avoiding disk clutter, and reducing security risks from unintended file retrieval. This is commonly used in web scraping, automated testing, and monitoring tools where file downloads are unnecessary or problematic. By configuring Chrome's command-line flags and browser settings properly, you can fully suppress download behavior without affecting page navigation or content loading.

What You'll Need

  • Chrome or Chromium installed (version 59 or later)
  • Basic command-line access
  • A script or tool that launches Chrome (e.g., Puppeteer, Selenium, or direct command-line usage)
  • Administrative access if modifying system-wide settings or policies

Step-by-Step

  1. Launch Chrome in headless mode using the --headless flag. This runs Chrome without a visible UI, essential for automation environments. Example: google-chrome --headless.
  2. Add the --disable-gpu flag when using headless mode, especially on Windows and older versions. While not always required, it prevents rendering issues. Command becomes: google-chrome --headless --disable-gpu.
  3. Prevent downloads by disabling the default download behavior using --disable-default-apps. This stops bundled apps from triggering downloads, though it’s not the primary download control.
  4. Set the download behavior by adding --disable-background-networking. This limits background activity that could initiate downloads without user interaction.
  5. Use --disable-client-side-phishing-detection to reduce background checks that might trigger network activity resembling downloads.
  6. Block file access entirely by including --disable-file-system. This restricts access to the local file system, reducing risk from malicious scripts attempting to save files.
  7. Set the download directory to a non-writable or virtual location using --download-default-directory=/dev/null (Linux/macOS) or --download-default-directory=NUL (Windows). This ensures any attempted download has nowhere valid to write.
  8. Add --safebrowsing-disable-download-protection to disable Chrome’s built-in download scanning. While this reduces security, it prevents download interruptions during automation. Use only in trusted environments.
  9. Enable --no-sandbox only if required by your environment, but avoid it in production due to security implications. It may be needed in some containerized or restricted systems.
  10. If using Puppeteer, configure the browser launch settings with the flags above. Example:
    const browser = await puppeteer.launch({
      headless: true,
      args: [
        '--headless',
        '--disable-gpu',
        '--download-default-directory=/dev/null',
        '--disable-background-networking',
        '--disable-client-side-phishing-detection',
        '--disable-default-apps',
        '--disable-file-system'
      ]
    });
  11. For Selenium, pass the options object with the same arguments:
    from selenium import webdriver
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--download-default-directory=/dev/null')
    options.add_argument('--disable-background-networking')
    driver = webdriver.Chrome(options=options)
  12. Verify the configuration by navigating to a page with a direct file link (e.g., a PDF or ZIP). If no download occurs and no temporary files appear in system directories, the settings are effective.

Common Mistakes

  • Forgetting to set --download-default-directory, allowing Chrome to use the default Downloads folder.
  • Using --headless=new or --headless=chrome without adjusting other flags, leading to inconsistent behavior across versions.
  • Overlooking background networking flags, which can allow silent downloads via service workers or preloads.
  • Assuming disabling the UI disables downloads—headless mode alone does not stop automatic downloads.
  • Adding too many unnecessary flags, which can cause Chrome to fail to start or behave unpredictably.

Tips

  • Test your configuration in a clean environment to ensure no residual browser policies interfere.
  • Use absolute paths for --download-default-directory to avoid ambiguity.
  • Combine with --disable-plugins and --disable-extensions to further reduce attack surface and unexpected behavior.
  • Monitor system logs or use tools like lsof (Linux/macOS) to detect any file write attempts during test runs.
  • Keep Chrome updated to ensure flag compatibility, as Google frequently changes or deprecates command-line options.

Troubleshooting

If downloads still occur, first verify that all relevant flags are included and correctly spelled. Typos in flag names (e.g., --disble-gpu) are common and cause options to be ignored. Check the active Chrome process using ps aux | grep chrome (Linux/macOS) to confirm flags are applied. If using Puppeteer or Selenium, ensure arguments are passed through the correct method (e.g., args array in Puppeteer, add_argument() in Selenium).

If Chrome fails to launch, remove flags one by one to identify the problematic one. Some flags conflict in certain environments. Also, ensure the target directory for --download-default-directory exists and is accessible—even when using /dev/null, incorrect paths can cause startup errors.

If downloads are blocked but network activity spikes, consider adding --disable-sync and --disable-translate to reduce background services. Use Chrome’s DevTools Protocol (via Puppeteer) to monitor network requests and identify unexpected download triggers.

Frequently Asked Questions

Does headless mode automatically block downloads?

No. Headless mode only removes the UI; downloads can still occur unless explicitly disabled via flags or settings.

Can I allow some downloads but block others?

Yes, but not through command-line flags alone. Use Puppeteer or Selenium to intercept requests and selectively block or allow downloads based on URL patterns.

Is it safe to disable download protection?

Only in controlled environments. --safebrowsing-disable-download-protection disables malware scanning, so use it only when processing known-safe content.

Why are files still appearing in /tmp?

Some temporary files may be created during page load. Use --disk-cache-dir=/dev/null and ensure no script explicitly saves content.

Do these settings work in Docker containers?

Yes, but ensure the container has proper permissions and /dev/null is available. Use minimal Chrome images like alpine-chrome for best results.

Can I disable downloads via Chrome policies instead?

Yes, in enterprise environments. Use the DownloadRestrictions policy set to 3 to block all downloads, but this requires administrative control.