Newer Version Available

This content describes an older version of this product. View Latest

closeTab() for Lightning Experience

Closes a workspace tab or subtab. This method works only in Lightning console apps.

Arguments

The method provides the same argument for both Aura Components and Lightning Web Components (LWC).

If you’re using the LWC Workspace API (Beta), Lightning Web Security must be enabled in the Salesforce org. LWC Workspace API is a Beta Service. Customer may opt to try such Beta Service in its sole discretion. Any use of the Beta Service is subject to the applicable Beta Services Terms provided at Agreements and Terms.

Note

Name Type Description
tabId string ID of the workspace tab or subtab to close.

LWC Sample Code

This component checks if it’s in a Lightning console app, returns information about the focused tab and closes it.

1import { LightningElement, wire } from 'lwc';
2import { IsConsoleNavigation, getFocusedTabInfo, closeTab } from 'lightning/platformWorkspaceApi';
3
4export class FocusedTabInfoExample extends LightningElement {
5    @wire(IsConsoleNavigation) isConsoleNavigation;
6
7    handleClick() {
8        if (this.isConsoleNavigation) {
9            getFocusedTabInfo().then((tabInfo) => {
10                closeTab(tabInfo.tabId);
11            }).catch(function(error) {
12                console.log(error);
13            });
14     }
15  }
16}

Aura Components Sample Code

This component has a button that, when pressed, closes the currently focused tab.

Component code:

1<aura:component implements="flexipage:availableForAllPageTypes" access="global">
2    <lightning:workspaceAPI aura:id="workspace"/>
3    <lightning:button label="Close Focused Tab" onclick="{!c.closeFocusedTab}"/>
4</aura:component>

Controller code:

1({
2    closeFocusedTab : function(component, event, helper) {
3        var workspaceAPI = component.find("workspace");
4        workspaceAPI.getFocusedTabInfo().then(function(response) {
5            var focusedTabId = response.tabId;
6            workspaceAPI.closeTab({tabId: focusedTabId});
7        })
8        .catch(function(error) {
9            console.log(error);
10        });
11    }
12})

Response

This method returns a promise that, upon success, resolves to true.