cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Delete JSON file after parsing

sclement1
4-Participant

Delete JSON file after parsing

All,

I'm attempting to make a service on a FileRepository Thing that loads a JSON file, parses it and then deletes the file when finished.  I am unable to get DeleteFile to work after using the loadJSON service.  Thingworx seems to hold the file open for quite a while before releasing it.  I've tried setting the variable I load the JSON into to a blank string and I've tried deleting the variable.  I've also tried restarting the thing using me.RestartThing().  I've also tried creating a completely separate service to delete the file.  This did not work either.  The only way the file seems to get released is with time.  Is there anyway to force Thingworx to let go of the file so it can be deleted?  The code I am using in the service that only deletes the files is:

/* Get list of all files in the repository folder*/

var params = {

  path: "" /* Default path.  Change if storing in a different location */,

        nameMask: undefined /* STRING */

    };

var jsonFileList = me.GetFileListing(params);

  for (var jsonCount = 0; jsonCount < jsonFileList.length;jsonCount++)

    {

        if (jsonFileList[jsonCount].name.indexOf("error") == -1)

        {

        var fileParams = {

  path: jsonFileList[jsonCount].name  /* STRING */

  };

        me.DeleteFile(fileParams);

            var result = jsonFileList[jsonCount].name;

        }

    }

This works fine as long as another service has not loaded the file using loadJSON.  Any help would be appreciated.  I'm using Thingworx 7.4.0-b159

1 ACCEPTED SOLUTION

Accepted Solutions
sclement1
4-Participant
(To:sclement1)

This answer was correct.  The files are held open for about a minute after they are accessed.  From what I've seen even getting a listing of the files in the folder is enough to prevent the files from being deleted.  As a work around I create an infotable of the files and then process them.  The first thing I do when I access the function is attempt to delete all the files in the infotable.  I also keep a datetime of the last time files were processed so I don't reprocess any that were not able to be deleted.  They just end up back in the delete list.  As long as you don't get a file listing of the directory more than once a minute the files should be deleted the next time the function runs.

View solution in original post

2 REPLIES 2
posipova
20-Turquoise
(To:sclement1)

I don't think there is a forced "unloadJson" method at the moment or an alternative to it. I've just engaged R&D to confirm that and to see the exact time on the lock, and as a next step I will place an enhancement request to introduce a solution to this.

sclement1
4-Participant
(To:sclement1)

This answer was correct.  The files are held open for about a minute after they are accessed.  From what I've seen even getting a listing of the files in the folder is enough to prevent the files from being deleted.  As a work around I create an infotable of the files and then process them.  The first thing I do when I access the function is attempt to delete all the files in the infotable.  I also keep a datetime of the last time files were processed so I don't reprocess any that were not able to be deleted.  They just end up back in the delete list.  As long as you don't get a file listing of the directory more than once a minute the files should be deleted the next time the function runs.

Top Tags