Skip to content

Instantly share code, notes, and snippets.

@sriedmue79
Last active June 12, 2024 06:51
Show Gist options
  • Save sriedmue79/27d571ad8ffd9218fcb1742c1166e79c to your computer and use it in GitHub Desktop.
Save sriedmue79/27d571ad8ffd9218fcb1742c1166e79c to your computer and use it in GitHub Desktop.
IBM i - Delete files from an IFS path based on their "last accessed" timestamp
--
-- Description: Delete IFS files within a particular path which have not been accessed in the past 3 months.
-- This example does not include subtrees, but that can be accomplished by changing the subtree option from 'NO' to 'YES'
--
--This query will list the files in the specified path which haven't been accessed in at least 3 months, along with a sample RMVLNK command string
SELECT PATH_NAME, ACCESS_TIMESTAMP, ('RMVLNK ''' || PATH_NAME || '''') AS CMD
FROM TABLE (
QSYS2.IFS_OBJECT_STATISTICS(START_PATH_NAME => '/my/path', SUBTREE_DIRECTORIES => 'NO')
)
WHERE ACCESS_TIMESTAMP < CURRENT DATE - 3 MONTHS
AND PATH_NAME <> '/my/path';
--Review the results of your query above. Only proceed with the query below after confirming that the desired files/paths are being targeted.
stop;
stop;
--CAUTION!!!
--This query will DELETE FILES in the specified path which haven't been accessed in at least 3 months
CREATE TABLE QTEMP.DLTFILE AS
(SELECT PATH_NAME, ACCESS_TIMESTAMP, ('RMVLNK ''' || PATH_NAME || '''') AS CMD
FROM TABLE (
QSYS2.IFS_OBJECT_STATISTICS(START_PATH_NAME => '/my/path', SUBTREE_DIRECTORIES => 'NO')
)
WHERE ACCESS_TIMESTAMP < CURRENT DATE - 3 MONTHS
AND PATH_NAME <> '/my/path')
WITH DATA;
--Review the results of the deletion:
SELECT * FROM QTEMP.DLTFILE;
--Check for any failures:
SELECT * FROM QTEMP.DLTFILE WHERE CMDRESULT<>1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment