I found another way to deal with this problem, which was better for me based on this answer . In my case, I use a parallel loop and do not want to delete all files from the temporary directory because it can delete temporary files from other processes.
@RobertH's answer, which assumes the name of each individual temporary file name, is good, but I was not sure that it manually forces the raster to write even small files to the hard drive instead of using RAM and slow down the process (the raster documentation says that it writes only to disk if the file does not fit into RAM).
So, I created a temporary directory from a loop or a parallel process, bound to a unique name from the data that is processed in the loop, in my case the value is single@data $OWNER :
#creates unique filepath for temp directory dir.create (file.path("c:/", single@data $OWNER), showWarnings = FALSE)
Paste your processing code here, and then at the end of the loop delete the entire folder:
#removes entire temp directory without affecting other running processes unlink(file.path("c:/", single@data $OWNER), recursive = TRUE)
source share