Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:14:26+00:00 2026-06-07T09:14:26+00:00

NSFileManager removeItemAtURL performs only recursive deletion. There is no special treatment for non-empty directories.

  • 0

NSFileManager removeItemAtURL performs only recursive deletion. There is no special treatment for non-empty directories.

How can I remove only empty directories by testing the condition beforehand? There is contentsOfDirectoryAtPath to get a list of files, which may be empty, but that’s overkill.

Alternately, is there a function to remove it only if empty, like good ol’ rmdir? Ah, I could just call that…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:14:28+00:00Added an answer on June 7, 2026 at 9:14 am

    You have to use that if you want to stay in Foundation land, there is no other way.

    But if you do so you introduce a race condition: After you list the contents of your directory and before you remove it some other program could write a new file there which then gets deleted. So you have to either accept the fact that you are deleting folders including all their content or you have to look for a different API.

    You could use the POSIX rmdir function to achieve your goal like this:

    NSString *path = [url path];
    int result = rmdir( [path fileSystemRepresentation] );
    if (result == 0) // everything ok
    else // lookup error code from errno
    

    If the directory is not empty you get the error code ENOTEMPTY.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to find the parent directory of a path using NSFileManager
In a class such as NSFileManager there are 2 versions of practically every method.
NSDictionary* fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filename error:nil] From the file attribute keys, you can
I'm trying to remove specific files from a directory using NSFileManager. I would like
How I can save UIImage to file with NSFileManager ? Thank,
NSFileManager* fileManager = [NSFileManager defaultManager]; NSURL* url = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject]; NSLog(@%@,url); NSString*
I'm currently using NSFileManager setAttributes to change the permission of a directory. My problem
I'm trying to use NSFileManager to determine if an item is a file or
I try to get the last modification date of a file: NSFileManager *fm =
Apparently, NSFileManager is unable to delete files created by mkstemp() . Here's some test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.