Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a folder from the documentsDirectory with Swift

I have created a VideoAssets folder within documentsDirectory as below, which can contain one or more video assets:

file:///private/var/mobile/Containers/Data/Application/2E3H5FDD-825D-407A-A8BE-71CD540A6E15/Documents/VideoAssets/ae6e4f59be0bc5984b043e.mp4

Periodically it needs to be cleaned out. How can I either empty the folder OR remove the VideoAssets folder in it's entirety? There's a fair bit of info out there for creating folders, but not for removing one.


1 Answers

Just use try FileManager.default.removeItem(at: directoryUrl)

From Apple's docs:

A file URL specifying the file or directory to remove. If the URL specifies a directory, the contents of that directory are recursively removed. You may specify nil for this parameter.

https://developer.apple.com/documentation/foundation/filemanager/1413590-removeitem

like image 135
aasatt Avatar answered Sep 02 '25 17:09

aasatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!