Skip to main contentSkip to footer

Fatal error: Uncaught TypeError: flock): Argument #1 (Sstream) must be of type resource. Automatically delete cache files older than 15 days in cPanel cron job

Rupak Nepali
Share:
Fatal error: Uncaught TypeError: flock): Argument #1 (Sstream) must be of type resource. Automatically delete cache files older than 15 days in cPanel cron job

Today, we faced one unique challenge: one of the client websites was down, and the error message showed it could not write the cache folder.

Fatal error: Uncaught TypeError: flock): Argument #1 (Sstream) must be of type resource

Fatal error: Uncaught TypeError: flock): Argument #1 (Sstream) must be of type resource, bool given in /.../system/library/cache/file.php:51 Stack trace: #0
/.../system/library/cache/file.php(51): flock(false, 2) #1 /.../system/library/cache.php(53): Cache\File-›set('store', Array) #2
/.../admin/model/setting/store.php(47): Cache->set('store',Array) #3 /.../storage/modification/system/engine/loader.php(248): ModelSettingStore->getStores) #4 .../system/engine/proxy.php(47): Loader->{closure} (Array, Array) #5 /.../admin/controller/common/header.php(79): Proxy->_ _call(getStores', Array) #6 /.../storage/modification/system/engine/action.php(79): ControllerCommonHeader->index(Array) #7 /.../storage/modification/system/engine/loader.php(48): Action-
>execute(Object(Registry), Array) #8 /.../admin/controller/common/dashboard.php(89): Loader->controller('common/header') #9 .../storage/modification/system/engine/action.php(79)ControllerCommonDashboard->index#10/.../admin/controller/startup/router.php(26):Action-
>execute(Object(Registry), Array) #11 /.../storage/modification/system/engine/action. php(79): ControllerStartupRouter->index() #12 /.../system/engine/router.php(67): Action-
›execute(Object(Registry)) #13 /.../system/engine/router.php(56): Router->execute(Object(Action)) #14 /.../system/framework.php(172): Router->dispatch(Object(Action), Object(Action)) #15 /.../system/startup.php(104): require_once(/.') #16 /.../admin/index.php(22): start('admin') #17 {main} thrown in /.../system/library/cache/file.php on line 51

Checking line 51 at file.php, we find the following code:

public function set($key, $value) {
	$this->delete($key);
	$file = DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.' . (time() + $this->expire);
	$handle = fopen($file, 'w');
	flock($handle, LOCK_EX);
	fwrite($handle, json_encode($value));
	fflush($handle);
	flock($handle, LOCK_UN);
	fclose($handle);
}

Instantly, we thought of removing the files and folders in the cache folder. First, we checked how many files were there in the cache folder and found out that there

find /home3/webocreation/23storage | wc -l

The directory /home3/webocreation/23storage/ was currently using approximately 5,353,389 inodes. It appears that the majority of those files were located within the cache/template folder. So, first, we delete the cache/template folder content through a command as we have terminal access, but you can do it through FTP.

rm -rf /home3/webocreation/23storage/cache/template/*

Warning: This command permanently deletes all files and sub-directories in the cache folder. Double-check the path before running it.

After deleting the files inside the cache/template folder, the website is back again. The disk usage was 250GB; after deletion, it showed 120 GB. This raised a concern that we need to clean up the cache folder periodically and automatically.

Automatically delete cache files older than 15 days in cPanel cron job

To automatically remove files older than 15 days from your OpenCart system/storage/cache folder using a cron job in cPanel, follow these steps:

✅ Step-by-Step Guide to Set Up the Cron Job

1. Log into cPanel

  • Access your hosting account and open the cPanel dashboard.

2. Open Cron Jobs

  • In the Advanced section, click on Cron Jobs.

3. Add New Cron Job

  • Choose a schedule.
Cron Job Cpanel Opencart

4. Command to Enter

Replace /home/username/public_html/system/storage/cache/ with the full path to your OpenCart cache folder. Then, enter this command:

find /home3/webocreation/23/storage/cache/ -mindepth 1 -mtime +15 -exec rm -rf {} \;

Explanation:

  • find: Linux command to search for files.
  • -type f: Search for files only.
  • -mtime +15: Files older than 15 days.
  • -delete: Deletes the matching files.

✅ Final Example

Cronjob lists

🔒 Important Notes:

  • This deletes only files, not folders.
  • If you want to delete empty folders, add -type d -empty -delete as a second command.
  • Always test the path and back up important data before using automated deletion.

Let me know if you want to also:

  • Clean logs
  • Remove image cache (image/cache)
  • Exclude specific files or folders

Happy caching! 🚀

Comments

Join the conversation and share your thoughts

Leave a Comment

Your comment will be reviewed before publishing.

Be the first to comment on this post!

Innovation

Let's Make Something Amazing Together

We always provide our best creative ideas at the highest level. Tell us about your project and we will make it work.

InnovateBringing innovative solutions to complex problems
AutomateStreamlining processes through automation
DominateLeading the market with exceptional results
Build Smarter and Launch FasterEfficient development for rapid deployment