Clear iOS cache programmatically

Leo Wagner de Souza 691 Reputation points

How can I programmatically clean the app cache on iOS, so the user can restart the app like if it was a clean reinstall?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,393 questions
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,555 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vahid Ghafarpour 14,215 Reputation points

    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 {
            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:

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful **

    0 comments No comments