Last active
January 1, 2026 16:28
-
-
Save alexios-angel/678a8a7e29fd27a3ae082d9e132ff033 to your computer and use it in GitHub Desktop.
Delete All Page Rules for Domains in .txt File - Cloudflare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| Delete ALL Cloudflare Page Rules for each domain in domains.txt. | |
| Authentication uses only an API Token (no email needed). | |
| Usage: | |
| python delete_page_rules.py --api-token YOUR_TOKEN --domains-file domains.txt | |
| """ | |
| """ | |
| API permissions: | |
| Zone -> Zone -> Read | |
| Zone -> Page Rules -> Read | |
| Zone -> Page Rules -> Edit | |
| """ | |
| """ | |
| Copyright 2026 Alexios Angel | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of | |
| this software and associated documentation files (the “Software”), to deal in the | |
| Software without restriction, including without limitation the rights to use, copy, | |
| modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
| and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
| INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
| PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
| HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
| SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| """ | |
| import argparse | |
| import sys | |
| from typing import List, Optional | |
| from cloudflare import Cloudflare | |
| def read_domains(path: str) -> List[str]: | |
| domains = [] | |
| with open(path, "r", encoding="utf-8") as f: | |
| for line in f: | |
| line = line.strip() | |
| if line and not line.startswith("#"): | |
| domains.append(line) | |
| return domains | |
| def get_zone_id(client: Cloudflare, domain: str) -> Optional[str]: | |
| """Return zone ID for domain.""" | |
| try: | |
| zones = client.zones.list(name=domain, match="all") | |
| except Exception as e: | |
| print(f"[ERROR] zone lookup failed for {domain}: {e}", file=sys.stderr) | |
| return None | |
| for z in zones: # pagination object → iterate, not index | |
| return getattr(z, "id", None) or z.get("id") | |
| print(f"[WARN] No zone found for {domain}") | |
| return None | |
| def delete_page_rules(client: Cloudflare, zone_id: str, domain: str) -> int: | |
| """Delete all page rules for zone.""" | |
| try: | |
| rules = list(client.page_rules.list(zone_id=zone_id)) | |
| except Exception as e: | |
| print(f"[ERROR] Cannot list rules for {domain}: {e}") | |
| return 0 | |
| if not rules: | |
| print(f"[INFO] No page rules on {domain}") | |
| return 0 | |
| deleted = 0 | |
| for rule in rules: | |
| rule_id = getattr(rule, "id", None) or rule.get("id") | |
| pattern = None | |
| # Attempt to extract rule pattern display (nice to show what was deleted) | |
| try: | |
| if hasattr(rule, "targets"): | |
| tgt = rule.targets[0] | |
| constraint = getattr(tgt, "constraint", None) or tgt.get("constraint") | |
| if constraint: | |
| val = getattr(constraint, "value", None) or constraint.get("value") | |
| if val: | |
| pattern = val | |
| except Exception: | |
| pass | |
| try: | |
| client.page_rules.delete(rule_id, zone_id=zone_id) | |
| print(f"[DELETED] {domain}: {rule_id} ({pattern if pattern else 'unknown rule'})") | |
| deleted += 1 | |
| except Exception as e: | |
| print(f"[ERROR] Failed deleting rule {rule_id} on {domain}: {e}") | |
| return deleted | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Delete ALL Page Rules for Cloudflare domains.") | |
| parser.add_argument("--api-token", required=True, help="Cloudflare API Token (not Global Key)") | |
| parser.add_argument("--domains-file", "-f", default="domains.txt", help="File with domain list") | |
| args = parser.parse_args() | |
| # Cloudflare auth — TOKEN ONLY | |
| client = Cloudflare(api_token=args.api_token) | |
| domains = read_domains(args.domains_file) | |
| total = 0 | |
| for domain in domains: | |
| zone_id = get_zone_id(client, domain) | |
| if not zone_id: | |
| continue | |
| total += delete_page_rules(client, zone_id, domain) | |
| print(f"\nDone — Deleted {total} page rules total.") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment