How To delete files properly

I’ve created a job that deletes files that aren’t used. The problem is the method taking up alot of time and I want to know if this is the correct way of deleting a file?
(we want to free up space on the disk so its important that the files get deleted from the disk as well)

    private void DeleteUnusedFilesFromFolders(FolderCollection folders)
    {
        foreach (var subFolder in folders)
        {
            foreach (var fileId in subFolder.FileIDs)
            {
                var file = MediaArchiveFile.GetFile(subFolder.Files.FileIDs.FirstOrDefault(),
                    Solution.Instance.SystemToken);

                if (!IsFileBeingUsed(file))
                {
                    //delete file
                    file.Delete(Solution.Instance.SystemToken);
                }
            }
        }
    }


    private bool IsFileBeingUsed(MediaArchiveFile file)
    {
        var searchResult = ModuleMediaArchive.Instance.SearchService.FindFilePointers(file.ID);

        return searchResult.Count > 0;
    }

Litium version: 5

It is a way to check and delete of course but remember that if file is used in a TextEditor then no FilePointer is created so then your file would be lost.
You can schedule a weekly based night job to avoid having too many files stored and maybe not necessary to run it every night in that case .

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.