How to Delete Cache Programmatically?

I want to delete the cache files in my application programmatically.

How do i go about doing this in code?

I want to launch a service (which i already have set up to do this).

FYI - IDK why my acceptance rate is low. Ive accepted most or all of my answers.

1

1 Answer

I assume you're talking about the Context's cache directory?

This is one way:

File cacheDir = context.getCacheDir();

File[] files = cacheDir.listFiles();

if (files != null) {
    for (File file : files)
       file.delete();
}
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest