Creating a separate function to delete directories

Small refactoring to reuse some code.