Skip to content

Instantly share code, notes, and snippets.

@dryqin
Forked from Tatsh/delall-icloud-photos.py
Created January 21, 2021 17:57
Show Gist options
  • Save dryqin/fc407e722d8235e727f9246fe51450e3 to your computer and use it in GitHub Desktop.
Save dryqin/fc407e722d8235e727f9246fe51450e3 to your computer and use it in GitHub Desktop.
Use pyicloud to delete all photos from iCloud (may need to be run multiple times)
#!/usr/bin/env python
from __future__ import print_function
import json
import sys
from authentication import authenticate
from future.moves.urllib.parse import urlencode
if __name__ == '__main__':
username = sys.argv[1]
password = sys.argv[2]
smtp_username = smtp_password = smtp_host = smtp_port = smtp_no_tls = notification_email = None
print('Logging in')
icloud = authenticate(username, password, smtp_username, smtp_password, smtp_host, smtp_port, smtp_no_tls, notification_email)
photos = icloud.photos.all
operations = []
url = '{}/records/modify?{}'.format(icloud.photos._service_endpoint, urlencode(icloud.photos.params))
headers = {'Content-type': 'text/plain'}
seen_record_names = []
for photo in photos:
# Avoid duplicate operations
if photo._asset_record['recordName'] in seen_record_names:
continue
seen_record_names.append(photo._asset_record['recordName'])
mr = {'fields': {'isDeleted': {'value': 1}}}
mr['recordChangeTag'] = photo._asset_record['recordChangeTag']
mr['recordName'] = photo._asset_record['recordName']
mr['recordType'] = 'CPLAsset'
op = dict(
operationType='update',
record=mr,
)
operations.append(op)
# Limit to 100 photos at a time
if len(operations) >= 100:
post_data = json.dumps(dict(
atomic=True,
desiredKeys=['isDeleted'],
operations=operations,
zoneID={'zoneName': 'PrimarySync'},
))
print('Deleting 100 photos')
print(icloud.photos.session.post(url,
data=post_data,
headers=headers).json())
operations = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment