Skip to main content

Configuring automatic deletion of your codespaces

Inactive codespaces are automatically deleted. You can choose how long your stopped codespaces are retained, up to a maximum of 30 days.

By default, GitHub Codespaces are automatically deleted after they have been stopped and have remained inactive for 30 days.

However, because GitHub Codespaces incurs storage charges, you may prefer to reduce the retention period by changing your default period in your personal settings for GitHub Codespaces. For more information about storage charges, see "关于 GitHub Codespaces 的计费."

Note: Whether or not you have set a personal codespace retention period, it's a good idea to get into the habit of deleting codespaces that you no longer need. For more information, see "删除代码空间."

Automatic deletion happens irrespective of whether a codespace contains unpushed changes. To prevent automatic deletion of a codespace, just open the codespace again. The retention period is reset every time you connect to a codespace, and the retention countdown restarts when the codespace is stopped.

If a repository belongs to an organization, the organization owner may have set a retention period for the whole organization. If this period is less than the default retention period in your personal settings then the organization retention period will apply to codespaces you create for this repository. For more information, see "限制 codespace 的保持期."

Each codespace has its own retention period. You may, therefore, have codespaces with different retention periods. For example, if:

  • You created a codespace, changed your default retention period, then created another codespace.
  • You created a codespace using GitHub CLI and specified a different retention period.
  • You created a codespace for an organization-owned repository that has a retention period configured in the organization settings. The ownership of the codespaces you create is shown on the "Your codespaces" page.

Note: The retention period is specified in days. A day represents a 24-hour period, beginning at the time of day when you stop a codespace.

Setting a default retention period for your codespaces

  1. 在任何页面的右上角,单击个人资料照片,然后单击“设置”。

    Screenshot of a user's account menu on GitHub. The menu item "Settings" is outlined in dark orange.

  2. 在边栏的“代码、规划和自动化”部分中,单击“ Codespaces”。

  3. Under "Default retention period", enter the number of days for which you want your codespaces to be retained, by default, after they have been stopped.

    Screenshot of the "Default retention period" setting, currently set to 1 day. Next to the number of days is the "Save" button.

    You can set your default retention period between 0 and 30 days.

    Warning: Setting the period to 0 will result in your codespaces being immediately deleted when you stop them, or when they timeout due to inactivity. For more information, see "Setting your timeout period for GitHub Codespaces."

  4. Click Save.

When you create a codespace using GitHub CLI you can override this default. If you create a codespace in an organization that specifies a shorter retention period, the organization-level value overrides your personal setting.

If you set a retention period of more than a day, you'll be sent an email notification one day prior to its deletion.

Checking the remaining time until autodeletion

You can check whether a codespace is due to be automatically deleted soon.

When an inactive codespace is approaching the end of its retention period, this is indicated in your list of codespaces on GitHub at

Screenshot of a list of three codespaces. The third of these is labeled "Expiring in 4 days" which is highlighted with a dark orange outline.

Avoiding automatic deletion of codespaces

You may have a codespace that you want to keep for longer than the retention period defined in your personal settings. You can do this by using the "Keep codespace" option. When you select this option, your codespace will be retained indefinitely, until you delete it manually.

Note: The "Keep codespace" option is not available for organization-owned codespaces affected by an organization retention policy.

Codespaces incur storage costs, or consume your included storage allowance if the codespace is owned by your personal GitHub account. You should therefore be aware of the cost implications of storing codespaces indefinitely. For more information, see "关于 GitHub Codespaces 的计费."

  1. 在 的左上角,选择 ,然后单击“ Codespace”****,转到 上的“你的 codespace”页面。

  2. To the right of the codespace you want to exempt from automatic deletion, click , then click Keep codespace.

    Screenshot of the dropdown menu for an active codespace. The "Keep codespace" option has a tooltip saying "Expires 10 days after shutdown."

Codespaces that you have exempted from automatic deletion are indicated in your list of codespaces with the bookmark icon ().

Screenshot of a section of the codespaces list, showing a codespace labeled with the bookmark icon.

Setting a retention period for a codespace

如果已安装 GitHub CLI,则可以使用它来处理 GitHub Codespaces。 有关 GitHub CLI 的安装说明,请参阅 GitHub CLI 存储库

To set the codespace retention period when you create a codespace, use the --retention-period flag with the codespace create subcommand. Specify the period in days. The period must be between 0 and 30 days.

gh codespace create --retention-period DAYS

If you don't specify a retention period when you create a codespace, then either your default retention period, or an organization retention period, will be used, depending on which is lower. For information about setting your default retention period, click the "Web browser" tab on this page.

若要详细了解 GitHub CLI,请参阅“关于 GitHub CLI”。

Setting the retention period

You can set your default retention period in your web browser, on Alternatively, if you use GitHub CLI to create a codespace you can set a retention period for that particular codespace. For more information, click the appropriate tab above.

Checking whether codespaces will be autodeleted soon

You can check, in the Visual Studio Code desktop application, whether a codespace is due to be automatically deleted soon.

  1. 在 VS Code 的活动栏中,单击“远程资源管理器”图标。

    活动栏的屏幕截图。 “远程资源管理器”边栏图标(一个被圆圈覆盖的矩形)以橙色边框突出显示。


    1. 访问命令面板。 例如,通过按 Shift+Command+P (Mac)/Ctrl+Shift+P (Windows/Linux)。
    2. 键入:details
    3. 单击“Codespace: 详细信息”。
  2. Choose GitHub Codespaces from the dropdown menu at the top right of the Remote Explorer, if it is not already selected.

  3. Under "GITHUB CODESPACES," position the mouse pointer over the codespace that you're interested in. A pop-up box is displayed showing you information about the codespace.

    If the codespace is nearing the end of its retention period, a line is included telling when this period is due to expire.

    Screenshot of the "Remote Explorer" side bar. In the right-click menu for a codespace, "Expiring in 19 days" is highlighted with an orange outline.

Further reading