Category filter

Script to clear cache on macOS devices

Jump To

Cached data from website visits enhances online interactions by preserving useful information on users’ devices. Its major function is reducing dependence on server connectivity by keeping text, graphics, and data on the user’s device. This stored data reduces reliance on servers and speeds up access. However, periodically clearing the cached data and log files on your Mac will help protect your data and free up additional storage space. Moreover, deleting the app cache can also help fix issues with certain apps and ensure the smooth operation of your devices. Run this bash script using Hexnode’s Execute Custom Script action to delete all the cached data stored on your macOS devices.

Scripting language – Bash

File extension – .sh

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Clear all cache

Initially, the script clears the system cache which might consist of logs, caches, and diagnostic reports from various locations within the system. It can also include user caches that contain temporary files and data created for a smooth user experience.

After clearing system cache, the script clears browsing cache for different browsers such as Google Chrome, Safari, Opera browser and Mozilla Firefox. The script checks if the respective app is present on the device or not, before proceeding to delete its cached data. If the app is present, the script will clear its cached data such as GPU cache, visited links, session storage, and more.

Finally, the terminal history and application caches will be cleared from the system.

Notes:

  • This bash script takes effect for the currently logged in user.
  • Running scripts that delete cache or other data can lead to the loss of browsing history and preferences related to the browser. Please be noted of the data loss and hence back up important data, if any, before executing such scripts.
  • System paths or versions might change with updates. So, always verify the paths or directories used in the scripts.
  • The rm –rf command can cause irreversible data deletion.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository