4,152 questions
I think you can do it by clearing all URLs Something like this:
import Foundation
class CacheManager {
static func clearAppCache() {
// Define the cache directory path
guard let cacheDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
return
}
do {
// Get all files in the cache directory
let fileURLs = try FileManager.default.contentsOfDirectory(at: cacheDirectory, includingPropertiesForKeys: nil, options: [])
// Delete each file
for fileURL in fileURLs {
try FileManager.default.removeItem(at: fileURL)
}
print("Cache cleared successfully!")
} catch {
print("Error clearing cache: \(error.localizedDescription)")
}
}
}
// Usage example:
CacheManager.clearAppCache()
** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful **