Skip to content

Instantly share code, notes, and snippets.

@Suleman-Elahi
Last active April 23, 2023 00:31
Show Gist options
  • Save Suleman-Elahi/3b6c1e02ead4c849ceb7e70955b55180 to your computer and use it in GitHub Desktop.
Save Suleman-Elahi/3b6c1e02ead4c849ceb7e70955b55180 to your computer and use it in GitHub Desktop.
Sript to unsave all saved Instagram posts. Uses Instagram private API by ping and its extensions to do the heavy lifting.
from instagram_private_api import Client, ClientCompatPatch
from instagram_private_api_extensions import pagination
import json, time
user_name = 'UserName'
password = 'PassWord'
api = Client(user_name, password)
items=[]
for results in pagination.page(api.saved_feed, args={}):
if results.get('items'):
items.extend(results['items'])
print("Starting unsaving posts.....")
for i,x in enumerate(items):
z = items[i]['media']['pk']
api.unsave_photo(media_id=z)
time.sleep(3)
print("Unsaved: ", z)
@Suleman-Elahi
Copy link
Author

Usage:

Install API modules first by running:

- pip install git+https://git@github.com/ping/instagram_private_api.git@1.6.0
- pip install git+https://git@github.com/ping/instagram_private_api_extensions.git@0.3.9

Run after adding your username and password at line 5 and 6

  • python3 unsaver.py

Sample output:

image

@hmble
Copy link

hmble commented Oct 22, 2020

this script shows following error when post doesn't contain any caption.

Traceback (most recent call last):
  File "unsave.py", line 20, in <module>
    z = items[i]['media']['caption']['media_id']
TypeError: 'NoneType' object is not subscriptable

A simple workaround for this is

- z = items[i]['media']['caption']['media_id']
+ z = items[i]['media']['pk']

@Suleman-Elahi
Copy link
Author

this script shows following error when post doesn't contain any caption.

Traceback (most recent call last):
  File "unsave.py", line 20, in <module>
    z = items[i]['media']['caption']['media_id']
TypeError: 'NoneType' object is not subscriptable

A simple workaround for this is

- z = items[i]['media']['caption']['media_id']
+ z = items[i]['media']['pk']

Thanks...

@N1kRolexx
Copy link

Works almost perfectly. Thanks!

Would be grateful if somebody could modify this to also delete videos and posts with multiple pictures.

@Suleman-Elahi
Copy link
Author

Suleman-Elahi commented Dec 21, 2021

@N1kRolexx Deletion would be another thing. This script is just for unsaving posts. For post deletion, there will be a different function in the API.

@N1kRolexx
Copy link

@Suleman-Elahi
Hi !
Thanks for reply!
I guess I did not express my thoughts correctly. The script currently does not unsave videos and posts with multiple pictures.

@SG-73
Copy link

SG-73 commented Jun 30, 2022

@Suleman-Elahi Could you please tell how to delete stories archive in one click?

@FetraRanaivoson
Copy link

Traceback (most recent call last):
File "C:\Users\ranai\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagram_private_api\client.py", line 523, in _call_api
response = self.opener.open(req, timeout=self.timeout)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 525, in open
response = meth(req, response)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 634, in http_response
response = self.parent.error(
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 563, in error
return self._call_chain(*args)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 496, in _call_chain
result = func(*args)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 643, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\ranai\Documents\unsaver.py", line 8, in
api = Client(user_name, password)
File "C:\Users\ranai\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagram_private_api\client.py", line 208, in init
self.login()
File "C:\Users\ranai\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagram_private_api\endpoints\accounts.py", line 49, in login
login_response = self._call_api(
File "C:\Users\ranai\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagram_private_api\client.py", line 527, in _call_api
ErrorHandler.process(e, error_response)
File "C:\Users\ranai\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagram_private_api\errors.py", line 123, in process
raise error_info['error'](
instagram_private_api.errors.ClientCheckpointRequiredError: checkpoint_challenge_required
PS C:\Users\ranai\Documents>

@Suleman-Elahi
Copy link
Author

@FetraRanaivoson you have either triggered the anti abuse detection system of Instagram or you have 2FA enabled.

@FetraRanaivoson
Copy link

@Suleman-Elahi Ok, I have already disabled 2FA yesterday. So how could I solve this? IG keeps notify me to change my password each time ...

@Suleman-Elahi
Copy link
Author

@Suleman-Elahi Ok, I have already disabled 2FA yesterday. So how could I solve this? IG keeps notify me to change my password each time ...

You will have to wait for a few days...or you can use VPN with server in the same country as you. Or, if you can get your ISP to give you a new IP address then perhaps it can be solved.

@Suleman-Elahi
Copy link
Author

@GetIt7788 open terminal in the folder where the unsaver.py file. And then run it like this python unsaver.py if it errors then tell me. It should work though.

@Suleman-Elahi
Copy link
Author

403 forbidden likely cased for abusing the API. Try after using a VPN and make sure that account doesn't have 2FA enabled.

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