My Magento 2 website is taking up so much hard drive space unnecessarily on my server. Over 50GB’s right now which is just crazy. When I tracked it down to a few folders, I found that it’s mainly the Captcha function and also the Cache and Page Cache. How to solve this problem permanently? 
Media Captcha Delete Expired images in Magento 2


Option 1 

You are right about the number of files and space occupied by the Magento folders, particularly Captcha images. When I checked, the folder size is over 10 GB, this is not expected to occur. In order to free up disk space and keep file nodes within the limit, we need to remove the folders and files under media/captcha. However this will be a temporary work around though it is a quick solution. The expired images will pile up again after a few days.

Option 2

Is there any problem with Magento cron job? I verified that the Magento cron job has been setup correctly, However the cron job is the main thread and it does not allow to address the Media Captcha files piling up issue. So the correct solution is to install the CLI tool called "MAGERUN" that allows us to perform the action "Delete Expired Captcha images" individually. As you know Captcha is an important security feature to prevent brute force attack attempts from hackers and secure the website. This is the correct method to flush the Captcha folder and remove only expired images. Removing folders and files using option 1 will cause login issue with admin user and customers

Step 1

Download the Mage Run tool and add cron job And save it under /path_to_mangeto_root/bin folder.

Step 2

Run the command to delete expired captcha images n98-magerun sys:cron:run captcha_delete_expired_images n98-magerun sys:cron:run captcha_delete_old_attempts

Step 3

To be safe, it is better to add a separate cron job to run and purge them automatically on a weekly basis.

php -d memory_limit=-1 /path_to_mangeto_root/bin/n98-magerun2.phar sys:cron:run captcha_delete_expired_images