Python Forum
Delete multiple comments with a single API call (facebook) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Delete multiple comments with a single API call (facebook) (/thread-35711.html)



Delete multiple comments with a single API call (facebook) - Ascalon - Dec-04-2021

Hi, currently to delete comments on posts in my facebook page I'm using the following code, which deletes one comment at a time, that is making an API call for each comment to delete.

    import facebook, requests
    
    page_id = ''
    post_id = ''
    page_token = ''
    graph = facebook.GraphAPI(page_token)
    
    post = graph.get_objects(ids = [page_id + '_' + post_id], fields = "comments")[page_id + '_' + post_id]['comments']

    comments = post['data'] # first 25 comments
    while 'next' in post['paging'].keys(): # while there is a page with new comments
        post = requests.get(post['paging']['next']).json() # download the next page
        comments += post['data']

    to_delete = []
    for comment in comments:
        if (...something...):
            to_delete.append( comment['id'] )

    for comment_id in to_delete:
        graph.delete_object(id = comment_id)
This require about 1 second for each comment, which can take up to hours if the comments are thousands. Is there a way to delete all the comments with a single API call? Maybe using the requests package?

It should be possible since I read it something similar in this page https://developers.facebook.com/docs/graph-api/batch-requests