Swift 3 version as extension:
extension FileManager {
func clearTmpDirectory() {
do {
let tmpDirectory = try contentsOfDirectory(atPath: NSTemporaryDirectory())
try tmpDirectory.forEach {[unowned self] file in
let path = String.init(format: "%@%@", NSTemporaryDirectory(), file)
try self.removeItem(atPath: path)
}
} catch {
print(error)
}
}
}
Example of usage:
FileManager.default.clearTmpDirectory()
Thanks to Max Maier, Swift 2 version:
func clearTmpDirectory() {
do {
let tmpDirectory = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(NSTemporaryDirectory())
try tmpDirectory.forEach { file in
let path = String.init(format: "%@%@", NSTemporaryDirectory(), file)
try NSFileManager.defaultManager().removeItemAtPath(path)
}
} catch {
print(error)
}
}
[[NSFileManager defaultManager] removeItemAtPath:NSTemporaryDirectory() error:NULL];
? – Meldameldoh