Sample code for 30+ languages & platforms
Java

Outlook -- Delete Folder

See more Outlook Examples

Deletes an email folder.

Note: This example requires Chilkat v9.5.0.68 or greater.

This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Hashtable;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Use your previously obtained access token here:
        http.setAuthToken("MICROSOFT_GRAPH_ACCESS_TOKEN");

        // This example will delete the folder /Inbox/abc/subFolderC

        // Get the existing folder ID from the folder map created by this example
        Hashtable htFolderMap = new Hashtable();
        StringBuilder sbMap = new StringBuilder();
        sbMap.loadFile("qa_data/outlook/folderMap.xml", "utf-8");
        htFolderMap.addFromXmlSb(sbMap);

        String existingFolderId = htFolderMap.lookupStr("/Inbox/abc/subFolderC");
        if (existingFolderId == null) {
            System.out.println("Folder ID not found");
            return;
        }

        http.setUrlVar("folder_id", existingFolderId);

        String resp;
        try {
            resp = http.quickDeleteStr("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // A 204 response (with no response body) indicates success.
        if (http.getLastStatus() == 204) {
            System.out.println("Folder deleted.");
        } else {
            System.out.println("Folder not deleted.");
            System.out.println(resp);
        }
    }
}