KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Clean Duplicate Cache Folders
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: May 12, 2021

With the new architecture, sometimes duplicate cache folders can be generated. These may build up overtime and saturate the harddrive. Below is a utility method to automatically delete duplicate cached folders for the current database leaving any other cache folders from other databases alone.

The code locates all cache folders that belong to the current database and checks if the folder is currently in use, if the cache folder is not being used by any instances of 4D then it will be deleted.

C_COLLECTION($strSplit_col)
C_COLLECTION($folderList_col)
C_COLLECTION($dbFolders_col)
C_OBJECT($folder_ob)
C_OBJECT($curFolder_ob)
C_TEXT($folderPath_t;$folder_t;$cachePath_t;$instanceID_t;$dbName_t)
C_TEXT($inuse_t;$cmd_t)
C_TEXT($in_t;$out_t;$err_t;$pid_t)

If(Application type=4D Remote mode)
  $folderPath_t:=Get 4D folder(Database folder)
  $strSplit_col:=Split string($folderPath_t;  Folder separator;sk ignore empty strings)

  $folder_t:=$strSplit_col.pop()
  $cachePath_t:=$strSplit_col.join(Folder separator)

  $strSplit_col:=Split string($folder_t;"_";sk ignore empty strings)
  $instanceID_t:=$strSplit_col.pop()
  $dbName_t:=$strSplit_col.join("_")

  $folder_ob:=Folder(Convert path system to POSIX($cachePath_t))
  $folderList_col:=$folder_ob.folders()
  $dbFolders_col:=$folderList_col.query("fullName = :1 AND \
    fullName != :2";$dbName_t+"@";$folder_t)

  For each($curFolder_ob;$dbFolders_col)
    If (Test path name($curFolder_ob.platformPath+"inuse.txt")=Is a document)
      $inuse_t:=Document to text($curFolder_ob.platformPath+"inuse.txt")       
      $pid_t:=Substring($inuse_t;0;Position(Char(Carriage return);$inuse_t)-1)
      $cmd_t:="TASKLIST /FI \"PID eq "+$pid_t+"\""
      SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")
      LAUNCH EXTERNAL PROCESS($cmd_t;$in_t;$out_t;$err_t)

      If (Position($pid_t;$out_t)=00)
        $curFolder_ob.delete(Delete with contents)
      End if
     Else
      $curFolder_ob.delete(Delete with contents)
    End if
  End for each
End if

Commented by Tai Bui on May 12, 2021 at 9:45 AM
Hi Kirk, Thank you for the catch. The code has been updated with the correct variable name.
Commented by Kirk Brooks on May 12, 2021 at 9:42 AM
There is a typo. $curFolder should be $curFolder_ob.