---
url: >-
  /statelake/scripting-reference/functions-and-procedures/file-functions/purgefiles.md
---

# PurgeFiles

The **PurgeFiles** function deletes files from the file directory specified by **AFileMask**, where a file date stamp is older the current date by **AOlderThanDays** number of days.

Setting **aIgnoreError** to the boolean value of **True** means that any issues encountered during deleting will simply be ignored - such as if a file cannot be deleted or a directory does not exist. It will just fail silently.

Setting **aIgnoreError** to the boolean value of **False** means that any issues encountered during deleting will be thrown back and the delete suspended.

::: warning
Take extreme care when using this function.

This function **permanently** deletes the files - they do not just go to the recycle bin.
:::

**Declaration**: Function PurgeFiles(const AFileMask : string; AOlderThanDays : word; aIgnoreError : boolean) : integer

**AOlderThanDays** can be a large value to represent files that are years old.

```pascal
procedure ScriptEvent (var Value : variant);
begin
  LogInfo('');
  LogInfo('Number of Files Deleted : '+IntToStr(PurgeFiles('C:\Test\SampleFiles\*.*',2920, False)));
end;
```

In the example above, there are 11 files with the following dates -

1. 12/01/2015
2. 05/02/2024
3. 20/11/2023
4. 05/11/2020
5. 24/10/2023
6. 22/01/2024
7. 12/01/2015
8. 20/05/2021
9. 07/09/2020
10. 21/01/2016
11. 05/02/2024

On the day that the **action** above was run, only three (3) files were deleted - file number 1, 7, and 10 only.
