Argument list too long when deleting files: /usr/bin/find

Challenge:
If you have a directory with tens of thousands of files in it and hitting the “Argument list too long error ” when trying to delete files within the directory, utilize the find command coupled with -exec rm {} as shown below.

Solution:

To remove all files in directory:

/usr/bin/find /directory/location -exec rm {} \;

To remove all files in directory older than 30 days:

/usr/bin/find /directory/location -mtime +30 -exec rm {} \;

Schedule in Cron via the following example to remove all files older than 30 days running every day at 8pm at night.

0 20 * * 0-6 /usr/bin/find /directory/location -mtime +30 -exec rm {} \;