public static function Delete( string $folder, bool $clear = false )
   {

      if ( empty( $folder ) && ! \is_dir( $folder ) )
      {
         // No folder = we are done here
         return;
      }

      // Ensure the folder ends with an directory separator.
      $folder = \rtrim( $folder, '\\/' ) . \DIRECTORY_SEPARATOR;

      // Start getting all elements of the defined folder

      $openDir = \opendir( $folder );
      // ignore . and .. (its always the first and second
      \readdir( $openDir ); \readdir( $openDir );
      while ( false !== ( $item = \readdir( $openDir ) ) )
      {
         // The absolute item path
         $path = $folder . $item;
         if ( !\is_dir( $path ) )
         {
            // $path is a file
            File::Delete( $path );
         }
         else
         {
            // $path is a folder. Delete it recursively
            static::Delete( $path );
         }
      }
      \closedir( $openDir );

      if ( ! $clear )
      {
         // $folder should not be deleted, only emptied. We are done here
         return;
      }

      // Finally delete $folder
      try { \rmdir( $folder ); }
      catch ( \Throwable $ex )
      {
         throw new IOException(
            $folder,
            'Could not delete the defined folder.',
            \E_USER_ERROR,
            $ex
         );
      }

   }