Skip to content

Instantly share code, notes, and snippets.

@Tatsh
Last active December 17, 2023 03:40
Show Gist options
  • Save Tatsh/0b5a10a2d25f6c8466290401d2026055 to your computer and use it in GitHub Desktop.
Save Tatsh/0b5a10a2d25f6c8466290401d2026055 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 = []
@joryan44
Copy link

joryan44 commented Mar 9, 2018

Complete Python newbie here but delete photos from iCloud to get a clean start is what I want to do.

so here is how I got where I am

  1. copy the delete script to /usr/local/bin (where home-brew lives, that is upstairs in /usr/local) Okay, I am also a newcomer to anything behinds the scenes on MacOS - despite copious years of experience with *nix.

  2. I download, build and install pyicloud on to iMac 10.13.3. I use sudo to install because I am in a hurry, naturally though it might be more prudent make a separate install of python altogether. Could this be my problem? The Apple installed version of Python is 2.7.10

  3. at any rate, the install, like the build (download and so on) goes smoothly , no obvious errors of a show stopping kind.

  4. attempt to run dellall and it promptly complains that authenticate module is not found.

  5. I scour docs.python.org for clues and come up empty

  6. I conclude I have ahem obviously omitted some necessary.

  7. need some help.

--Captain Obvious

@joryan44
Copy link

joryan44 commented Mar 9, 2018

Installed the 'real' Python for MacOS and along the way, TclTK ( a compatible version). Tweaked my .bashrc to avoid unhappiness resulting from relying on older versions and am ransacking the literature for more clues ...

@joryan44
Copy link

joryan44 commented Mar 9, 2018

bugger me for setting up two-factor authentication!

@joryan44
Copy link

joryan44 commented Mar 9, 2018

need to rethink approach??

@ndbroadbent
Copy link

ndbroadbent commented Mar 18, 2018

Hi @joryan44, I just found this, and I think they were using my icloud_photos_downloader project as the base for this script. I have an authentication.py file in there with an authenticate method, and that takes care of two-factor authentication.

I would try cloning the repo, downloading the script into there, and then running it:

git clone https://github.com/ndbroadbent/icloud_photos_downloader
cd icloud_photos_downloader
curl -sfLO https://gist.github.com/Tatsh/0b5a10a2d25f6c8466290401d2026055/raw/77d23ca0d1448f8e8eb4f208dfcfe84781d98d67/delall-icloud-photos.py
python delall-icloud-photos.py

(Note: I haven't tried the script)

@leftkark
Copy link

leftkark commented Jul 7, 2021

is this still working or does anyone know any other way to delete all icloud photos instead of waiting 30 days before automatically deleted by icloud? I have deleted them from my ios device first but need to wait 30 days to be removed from my icloud account.

@anupammukherjee
Copy link

anupammukherjee commented Jan 2, 2022

Hi, combining the previous comments and making some further changes, the following process worked for me:

git clone https://github.com/ndbroadbent/icloud_photos_downloader
cd icloud_photos_downloader
cd icloudpd
curl -sfLO https://gist.github.com/Tatsh/0b5a10a2d25f6c8466290401d2026055/raw/77d23ca0d1448f8e8eb4f208dfcfe84781d98d67/delall-icloud-photos.py

Change the line:

icloud = authenticate(username, password, smtp_username, smtp_password, smtp_host, smtp_port, smtp_no_tls, notification_email)

to
icloud = authenticate(username, password)

Install icloudpd dependencies by installing icloudpd (Its documentation talks about how to do this)
Finally, run:
python delall-icloud-photos.py

The first time you run this, it will perform two factor authentication.
It will work well afterwards.

@sfdcai
Copy link

sfdcai commented Jan 30, 2023

i am getting below error
File "delall-icloud-photos.py", line 10, in
username = sys.argv[1]
IndexError: list index out of range

@zakirangwala
Copy link

I just had a similar issue. Made a quick script to do it :
https://github.com/zakirangwala/icloud-photo-manager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment