Skip to content

Instantly share code, notes, and snippets.

@derekphilipau
Created January 4, 2024 03:39
Show Gist options
  • Save derekphilipau/a06284488f02e970c384c2f61e2a3403 to your computer and use it in GitHub Desktop.
Save derekphilipau/a06284488f02e970c384c2f61e2a3403 to your computer and use it in GitHub Desktop.
Delete all Cloudflare images
# Delete all Cloudflare images
# Useful if migrating to a different images provider
# Unfortunately Cloudflare still doesn't offer bulk delete
import requests
# Replace with your actual account and authorization details
account_identifier = "youraccountid"
api_token = "yourapitokenhere"
def list_images():
all_images = []
page = 1
while True:
url = f"https://api.cloudflare.com/client/v4/accounts/{account_identifier}/images/v1?page={page}&per_page=100"
headers = {
"Authorization": f"Bearer {api_token}"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
images = data['result']['images']
if not images:
break # Exit the loop if no more images are returned
all_images.extend(images)
page += 1
else:
print("Error listing images:", response.text)
break
return all_images
def delete_image(image_id):
url = f"https://api.cloudflare.com/client/v4/accounts/{account_identifier}/images/v1/{image_id}"
headers = {
"Authorization": f"Bearer {api_token}"
}
response = requests.delete(url, headers=headers)
if response.status_code == 200:
print(f"Image {image_id} deleted successfully")
else:
print(f"Error deleting image {image_id}:", response.text)
def main():
images = list_images()
for image in images:
delete_image(image['id'])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment