Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Mike
    RESTFUL Bulk Delete Incident AttachmentsAnswered
    Topic posted May 10, 2019 by MikeBronze Medal: 1,250+ Points, tagged REST 
    145 Views, 13 Comments
    Title:
    RESTFUL Bulk Delete Incident Attachments
    Summary:
    Is there a method to bulk delete attachments from incidents?
    Content:

    Referencing the API Guide I was able to figure out how to delete attachments from individual incidents. For example 

    curl -k -H 'OSvC-CREST-Application-Context:Delete incident attachments' -X DELETE "https://rest_user:xxxxxxxx@acme--tst.custhelp.com/services/rest/connect/v1.3/incidents/15339/fileAttachments/169050"

    I was curious if anyone know's of a way to perform a bulk delete of attachments but preserve the incident?

    Thanks 

    Mike 

    Version:
    18A

    Best Comment

    Lakshay Bhalla

    You can delete the attachements in bulk using a custom script with the help of a report which lists all you Incident ID, Ref no and a third column with definition like group_concat(fattach.file_id,',').In this report add filters to be specific for those incidents you wanna delete. In this Custom Script you can use the REST API to perform the bulk delete in a loop for each row of that report. In Custom script you will first loop through each incident ID and then next you will loop through each File Attachement ID (as these are having comma as seprator, You can deseprate them in php easily).

    Thanks.

    -Lakshay

    Comment

     

    • Lakshay Bhalla

      You can delete the attachements in bulk using a custom script with the help of a report which lists all you Incident ID, Ref no and a third column with definition like group_concat(fattach.file_id,',').In this report add filters to be specific for those incidents you wanna delete. In this Custom Script you can use the REST API to perform the bulk delete in a loop for each row of that report. In Custom script you will first loop through each incident ID and then next you will loop through each File Attachement ID (as these are having comma as seprator, You can deseprate them in php easily).

      Thanks.

      -Lakshay

    • Mike

      Thank you Lakshay,

      Unfortunately my CPM scripting knowledge is limited. Would you happen to have an example of this report with the custom script that would achieve this? 

      • Lakshay Bhalla

        Hi Mike,

        In report if you want to use filters what would they be to narrow down the no. of incidents, i.e, to be specific, which filters would suffice,like Ref no greater than or equal to or incident created on a particular date, etc.

        And It is not going to be a CPM but a Custom Script which you can write and store it in the file manager and then execute those scripts using browser or in CPMs.

        Thanks.

        • Mike

          Hi Lakshay , 

          I am filtering on specific incident attachments using ROQL query

          "SELECT ID, FileAttachments.ID AS FileID FROM incidents WHERE FileAttachments.CreatedTime <= '2015-01-31' ORDER BY ID"

          Thanks 

          • Lakshay Bhalla

            Hi Mike,

            Here is the report attached with this thread. In this report the report fetches all the attachements, from the incidents, which are created greater than or equal to a certain date.

            Along With report i am attaching the Code for it. Both are in a Zip Folder.

            In the Code replace the date and the Yoursite with your site name.

            And in cUrl call put your basic authorzation String which is composed of username and password can be created using Postman(easy method).

            Thanks

            -Lakshay

             

             

            • Mike

              Lakshay,

              Many thanks! This is tremendously helpful. I was able to remove 33,000 file attachments in about an hour! 

              Cheers,

              Mike 

            • Mike

              Just a quick question, 

              Does >= '2019-12-31' mean incident attachments newer or older than '2019-12-31' ? 

              I assume it means fattach.created newer than 2019-12-31 

              • Lakshay Bhalla

                Yes it means Attachments created greater than or equal to '2019-12-31', i.e. All the attachments created on 31 Dec 2019 and later than that date.

                • Kim Dante

                  So, I got excited a little too soon.  The script appeared to run, generated no errors, gave me a list of file attachment IDs that it supposedly deleted successfully.  BUT when I double-checked, it actually deleted nothing.  Any ideas on what could be wrong?

    • Kim Dante

      Lakshay,

       

      I just wanted to comment on how helpful this is.  I am a complete novice on PHP and custom scripts, but I was able to figure this out and delete old attachments in bulk freeing up a bunch of storage.  This community is awesome!!!

    • Kim Dante

      So, I got excited a little too soon.  The script appeared to run, generated no errors, gave me a list of file attachment IDs that it supposedly deleted successfully.  BUT when I double-checked, it actually deleted nothing.  Any ideas on what could be wrong?

    • Mike

      Hi Kim, attached is my version of a file attachment removal script. It uses python and ConnectPHP to remove the attachments. I strongly recommend testing this script in a non production environment before running it in production. 

      1. Edit d.php with your site info 

      2. edit fattach_remove.py with your REST Credentials and Site info 

      3. Enter Start/End Dates using UNIX EPOCH Time to fattach_remove.py like so.

      def main():

              start=1551502800

              end=1561065129