Created
July 11, 2026 16:00
-
-
Save 0x9900/6fde39bfd5654f44e14d6fdeeb86aba2 to your computer and use it in GitHub Desktop.
Purge cloudflare cache
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 python | |
| # vim:fenc=utf-8 | |
| # | |
| # Copyright (C) 2026 fred <github-fred@hidzz.com> | |
| # | |
| # Distributed under terms of the BSD 3-Clause license. | |
| import argparse | |
| import logging | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| import toml | |
| from cloudflare import Cloudflare, CloudflareError | |
| CONFIG_FILE = '.cloudflare.toml' | |
| logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') | |
| logging.getLogger('httpx').setLevel(logging.ERROR) | |
| @dataclass | |
| class Config: | |
| """Holds configuration informations""" | |
| class Error(Exception): | |
| pass | |
| api_token: str | None = None | |
| zone_id: str | None = None | |
| files: list[str] | None = None | |
| @classmethod | |
| def load(cls) -> None: | |
| """load token and developer_id from the config file""" | |
| config_path = Path(CONFIG_FILE) | |
| if not config_path.exists(): | |
| raise Config.Error('Configuration file missing') | |
| try: | |
| with config_path.open('r', encoding="utf-8") as cfd: | |
| _config = toml.load(cfd) | |
| except ValueError as err: | |
| raise Config.Error(f'Configuration error {err}') | |
| # Update instance attributes | |
| for key, value in _config.items(): | |
| if hasattr(cls, key): | |
| setattr(cls, key, value) # Assign values dynamically | |
| else: | |
| logging.warning('Unknown config attribute: "%s"', key) | |
| def purge_files(zone_id, api_token, files): | |
| try: | |
| client = Cloudflare(api_token=api_token) | |
| response = client.cache.purge(zone_id=zone_id, files=files) | |
| # If we get a response with an ID, it was successful | |
| logging.info("√ Files purged successfully!") | |
| logging.info(f" Purge ID: {response.id}") | |
| except CloudflareError as e: | |
| logging.error(f"✗ Cloudflare API error: {e}") | |
| except Exception as e: | |
| logging.error(f"✗ Unexpected error: {e}") | |
| def purge_cache(zone_id, api_token): | |
| try: | |
| client = Cloudflare(api_token=api_token) | |
| response = client.cache.purge(zone_id=zone_id, purge_everything=True) | |
| # If we get a response with an ID, it was successful | |
| logging.info("√ Cache purged successfully!") | |
| logging.info(f" Purge ID: {response.id}") | |
| except CloudflareError as e: | |
| logging.error(f"✗ Cloudflare API error: {e}") | |
| except Exception as e: | |
| logging.error(f"✗ Unexpected error: {e}") | |
| def main(): | |
| try: | |
| Config.load() | |
| except Config.Error as err: | |
| logging.error(err) | |
| return | |
| parser = argparse.ArgumentParser(description='Antenna Analysis Tool') | |
| group = parser.add_mutually_exclusive_group(required=True) | |
| group.add_argument('-A', '--all', action="store_true", | |
| help="Purge all files") | |
| group.add_argument('-f', '--files', action="store_true", | |
| help="Purge files") | |
| opts = parser.parse_args() | |
| if opts.all: | |
| purge_cache(Config.zone_id, Config.api_token) | |
| elif opts.files and not Config.files: | |
| logging.error('The file list in config file is empty') | |
| elif opts.files and Config.files: | |
| purge_files(Config.zone_id, Config.api_token, Config.files) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment