Below is the same dev.to post rewritten strictly using the markdown syntax you specified.
You can copy–paste this directly into dev.to.
Automating Facebook “Cancel Request / Unfollow” Using Pure JavaScript
Facebook does not provide a native way to bulk cancel follow requests or unfollow users. When the following list becomes large, manually clicking Cancel request one by one is inefficient and error-prone.
This post explains how to automate the process using pure JavaScript executed directly in the browser console — no extensions, no APIs, and no third-party libraries.
Problem Statement
Facebook’s UI presents several challenges:
- Infinite scrolling
- Obfuscated and frequently changing CSS class names
- Dynamically loaded DOM elements
Because of this:
- Static selectors break easily
- Automation must be DOM-aware
- Clicking too fast may trigger temporary restrictions
The objective was to create a script that is:
- Stable
- Rate-limit friendly
- Resilient to DOM changes
- Easy to stop safely
Solution Overview
The script uses a two-phase strategy.
Phase 1: Scroll to the End
- Scrolls until no new data loads
- Detects the true end by tracking document height
- Prevents infinite scrolling loops
Phase 2: Cancel Requests from Bottom to Top
- Collects all Cancel request / Unfollow buttons
- Processes them from bottom → top
- Clicks one button every 5 seconds
This approach avoids DOM reflow issues and ensures no request is skipped.
Why Bottom to Top Works Better
When Facebook removes an item from the list:
- The DOM reflows
- Element positions shift
- Indexes change dynamically
Processing from bottom to top ensures:
- Already-processed items are not affected
- Buttons are not skipped
- DOM behavior remains predictable
Bottom-to-top processing is significantly more reliable on infinite-scroll UIs.
Key Design Decisions
1. No CSS Class Selectors
Facebook class names change frequently.
Instead, the script relies on:
role="button"aria-label- Visible button text
These attributes are far more stable.
2. Rate-Limit Friendly Execution
- One action every 5 seconds
- No parallel clicks
- Human-like interaction pattern
This reduces the likelihood of triggering Facebook’s abuse detection.
3. Manual Stop Support
The script can be stopped at any time without refreshing the page.
Run this in the console:
window.__STOP_UNFOLLOW__ = true;
How to Use
- Open Facebook in Chrome
- Navigate to Following or Followers
- Open DevTools → Console
- Paste the script from the repository
- Keep the tab active
To stop execution:
- Refresh the page or
- Run the stop command shown above
Full Script Source
The latest maintained version of the script is available here:
facebook-auto-unfollow-cancel-request-script
Always prefer the GitHub version, as Facebook UI changes frequently.
Limitations and Disclaimer
- This is not an official Facebook tool
- Use at your own risk
- Do not run repeatedly in short timeframes
- Facebook may change its UI at any time
This project is intended for educational and personal automation purposes only.
Possible Improvements
Future enhancements could include:
- Auto-handling confirmation dialogs
- Randomized delays
- Daily action limits
- Pause / resume functionality
- Batch processing for very large lists
Contributions and suggestions are welcome.
Closing Thoughts
This project demonstrates what can be achieved using:
- Plain JavaScript
- Native DOM APIs
- Careful control flow
No browser extensions.
No automation frameworks.
No backend services.
If you find this useful, consider starring the repository or adapting the script for your own automation needs.
Top comments (0)