Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.StringBuilder,
  Chilkat.Hashtable;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  htFolderMap: THashtable;
  sbMap: TStringBuilder;
  existingFolderId: string;
  resp: string;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;

  //  Use your previously obtained access token here:
  http.AuthToken := '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 
  htFolderMap := THashtable.Create;
  sbMap := TStringBuilder.Create;
  sbMap.LoadFile('qa_data/outlook/folderMap.xml','utf-8');
  htFolderMap.AddFromXmlSb(sbMap);

  existingFolderId := htFolderMap.LookupStr('/Inbox/abc/subFolderC');
  if (htFolderMap.LastMethodSuccess <> True) then
    begin
      WriteLn('Folder ID not found');
      Exit;
    end;

  http.SetUrlVar('folder_id',existingFolderId);

  resp := http.QuickDeleteStr('https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}');
  if (http.LastMethodSuccess <> True) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  //  A 204 response (with no response body) indicates success.
  if (http.LastStatus = 204) then
    begin
      WriteLn('Folder deleted.');
    end
  else
    begin
      WriteLn('Folder not deleted.');
      WriteLn(resp);
    end;


  http.Free;
  htFolderMap.Free;
  sbMap.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.