Solved World Deletion

Discussion in 'Spigot Plugin Development' started by XZot1K, Jun 8, 2016.

  1. Im trying to delete a world folder but it is simply not working any ideas? heres my method:
    Code (Text):
        public static void deleteWorld(String name) {
            World world = plugin.getServer().getWorld(name);
            world.setAutoSave(false);
            File dir = world.getWorldFolder();
            plugin.getServer().unloadWorld(world, true);
            dir.delete();
        }
     
  2. MiniDigger

    Supporter

    you need to delete the folder recursively, meaning you need to go into the folder and into every subfolder and delete any file you find.
     
    • Agree Agree x 1
  3. In addition to what @MiniDigger said, I believe there's a FileUtils.deleteDirectory(File) method which deletes the specified file folder
     
  4. xps

    xps

    Code (Text):

    File filePath = new File(x);
    FileUtils.deleteDirectory(filePath);
     
     
  5. I got it took a few but i figured it out here is the solution (I didn't check for more than 2 sets of sub folders since its not needed for the world folders):
    Code (Text):
        public static void deleteWorld(String name) {
            World world = plugin.getServer().getWorld(name);
            world.setAutoSave(false);
            File dir = world.getWorldFolder();
            plugin.getServer().unloadWorld(world, true);
            for (File file : dir.listFiles()) {
                if (file.isDirectory()) {
                    for (File f : file.listFiles()) {
                        f.delete();
                    }
                    file.delete();
                } else {
                    file.delete();
                }
            }
            dir.delete();
        }
     
  6. If you use the below it'll do it recursively if you want to be 100% sure it gets deleted.

    Code (Text):
    public static void deleteFiles(File file) throws IOException{
            if( file.isDirectory() ){
                if( file.list().length==0 ){
                    file.delete();
                } else {
                    String files[] = file.list();
                    for (String temp : files) {
                        File fileDelete = new File(file, temp);
                        deleteFiles(fileDelete);
                    }
                    if(file.list().length==0){
                        file.delete();
                    }
                }
            } else {
                file.delete();
            }
        }
     
  7. MiniDigger

    Supporter

    why constructing a new file object if you can get file objects by replacing file.list() with file.listFiles()? ;)
     
  8. Seeeeeeee.... I know whats i doinnnnnn o_O:p
     
  9. Code (Text):

        public static void deleteWorld(File file) /*@param file is the World directory.*/
        {
            if(file.isFile())
            {
                file.delete();
            }
            else if(file.isDirectory())
            {
                for(File f : file.listFiles())
                {
                    deleteWorld(f);
                }

                file.delete();
            }
        }
    Just use a recursive method call here.
     
  10. Cause I made this ages back and didn't have a clue what I was doin ;) xD