• Documentation
  • Pricing
© 2026 Serverless, Inc. All rights reserved.

Framework

  • Overview
  • Documentation
  • Plugins360
  • Pricing

Learn

  • Blog
  • GuidesUpdated
  • Examples240
  • Courses

Resources

  • Support
  • Security
  • Trust Center
  • Status

Community

  • Slack
  • GitHub47k
  • Forum
  • Meetups

Company

  • About
  • Careers
  • Contact
  • Partners

Legal

  • Terms of Service
  • Privacy Policy
  • Trademark
  • DMCA
Serverless Framework Logo

Serverless Framework

Intro
SetupUpgrading To V4ConceptsTutorialAWS CredentialsLicense Keys
DeployingPackagingBuildingTestingDev ModeServicesFunctions
OverviewHTTP (API Gateway v2)REST (API Gateway v1)ActiveMQApplication Load BalancerAlexa SkillAlexa Smart HomeCloudWatch EventCloudWatch LogCloudFrontCognito User PoolEventBridge EventIoTIoT Fleet ProvisioningKafkaKinesis & DynamoDBMSKRabbitMQS3ScheduleSNSSQSWebsocket
LayersManaged InstancesWeb Apps (Lambda Web Functions)Sandboxes (Lambda MicroVMs)MCP ServersAlertsVersion PruningDomainsIAM Function PermissionsParameters
OverviewSelf-reference serverless.ymlServerless CoreEnvironment VariablesCLI OptionsExternal YAML/JSON FilesJavascript propertiesGitDoppler
OverviewS3 ObjectsSSM Parameter Store & Secrets ManagerCloudFormation Stack Outputs
OverviewVaultTerraform State Output
ResourcesComposing ServicesDeployment BucketStateAgent SkillsPython support
OverviewRuntimeGatewayMemoryBrowserCode InterpreterDev Mode
API Gateway Proxy
OverviewGeneral ConfigurationAuthenticationAPI KeysData SourcesResolversPipeline FunctionsSubstitutionsCachingDelta SyncCustom DomainWAFCLI Commands
Deploying SAM/CFN TemplatesWorkflow Tips
OverviewCreating PluginsCLI OutputCustom CommandsCustom VariablesExtending the Configuration schemaExtending and overriding configuration
OverviewDashboardAxiom
Overviewagent docsagent inspectagent setupagent skills installagent skills list / readdeploydeploy functiondeploy listdevdiffinfoinvokeinvoke localloginlogin awslogin aws ssologsmetricspackageplugin installplugin uninstallprintprunereconcileremoverollbackrollback functionusage
OverviewConcepts
OverviewMetricsTracesTroubleshoot
OverviewNode.jsPython
OutputsProviders
OverviewBranch DeploymentsPreview DeploymentsCustom ScriptsTestingPrivate PackagesNotificationsMono ReposDeploy in your own CI/CDBest PracticesTroubleshootingFAQ
OverviewSetupToolsAWS Integration
Serverless.yml Reference
Examples and TutorialsConfiguration Validation
  1. CLI Reference
  2. Prune

serverless prune

serverless prune deletes the old published versions of a deployed service's Lambda functions, and the old image versions of its sandboxes, keeping the --number newest of each. With --includeLayers it also prunes layer versions, and with --includeArtifacts it deletes deployment artifacts that no remaining version uses. Run it by hand with this command; to prune after every deploy, set custom.prune as the version pruning guide describes.

serverless prune --number 3

Warning: Deleted versions can't be restored, and serverless rollback function can't restore code from a deleted version. Preview the run with --dryRun --verbose first.

Options

serverless prune accepts these options:

OptionAliasValueDefaultRequiredDescription
--number-nnumberYesNumber of versions to keep besides $LATEST, aliased versions and a sandbox's live version.
--function-fstringAll functionsNoPrune only this function, as named under functions. Sandboxes are left alone.
--layer-lstringNoPrune only this layer, as named under layers. Without --includeLayers, ignored when --function is set.
--sandboxstringNoPrune only this sandbox's image versions, as named under sandboxes.
--includeLayers-ibooleanfalseNoAlso prune layer versions. See What serverless prune deletes for each combination.
--includeArtifacts-abooleancustom.prune.includeArtifacts, else falseNoAlso delete unused deployment artifacts. See Deployment artifacts. Skipped on scoped runs.
--dryRun-dbooleanfalseNoDelete nothing. With --verbose, list what would be deleted.
--verbosebooleanfalseNoPrint each version deleted, kept or selected.
--stage-sstringprovider.stage, else devNoStage to prune.
--region-rstringprovider.region, else us-east-1NoAWS Region of the stage.
--aws-profilestringprovider.profileNoAWS profile whose credentials the command uses.
--config-cfile pathserverless.ymlNoService configuration file to use.

The command also accepts the options every command takes: --param="key=value" (repeatable), --org, --app, --debug and --help (-h).

What serverless prune deletes

serverless prune chooses what to prune from the scope options:

OptionsPruned
NoneEvery function, and every sandbox's image versions.
--includeLayersEvery function and every layer, and every sandbox's image versions.
--functionThat function.
--function with --includeLayersThat function and every layer.
--layerThat layer.
--layer with --includeLayersThat layer and every function.
--function with --layerThat function; --layer is ignored.
--function, --layer and --includeLayersThat function and that layer.
--sandboxThat sandbox's image versions; --includeLayers is ignored.
--sandbox with --function or --layerWhat the same options prune without --sandbox, and that sandbox's image versions.

For each function, the command keeps $LATEST, every version a Lambda alias points to, and the --number newest other versions, and deletes the rest. Aliased versions don't count toward --number. A Lambda@Edge version that Lambda can't delete because it is replicated is skipped with a warning.

Layer versions

For each layer, serverless prune keeps the --number newest versions. An older version that is still attached to an existing version of one of this service's functions is kept too, even when the same run deletes that function version; the next run then deletes the layer version. Attachments from other services or accounts can't be detected, so a layer used only outside this service loses its old versions.

Sandbox image versions

For each sandbox, serverless prune keeps the image's live version plus --number older successful versions, and never deletes a version that a MicroVM still uses or that is still building. Failed versions are deleted without counting toward --number. A sandbox that isn't deployed is skipped. Pruning sandbox image versions needs lambda:GetMicrovmImage, lambda:ListMicrovmImageVersions, lambda:GetMicrovmImageVersion, lambda:ListMicrovms and lambda:DeleteMicrovmImageVersion; without them, the command stops with an error that names them. See Sandbox image versions in the pruning guide.

Deployment artifacts

With --includeArtifacts, or with custom.prune.includeArtifacts: true in serverless.yml, after pruning versions, serverless prune looks at the stage's deployment folders in the deployment bucket that are older than the newest provider.deploymentBucket.maxPreviousDeploymentArtifacts (default 5). A folder whose code no remaining function or layer version uses is deleted with plain S3 delete requests. Any folder the command can't prove unused is kept. For a service with sandboxes, sandbox zips that no remaining deployment points to are deleted, except zips uploaded within the last hour. The sweep is skipped on runs scoped with --function, --layer or --sandbox. See Artifact retention in reference mode.

Warning: On a versioned bucket (the Framework-managed deployment bucket, and any bucket used with codeStorageMode: reference), the delete requests write delete markers and the object versions stay recoverable. On an unversioned bucket (an in-stack deployment bucket without deploymentBucket.versioning: true, or a bucket you name that has versioning off), the deletion is permanent. Run with --dryRun --verbose first.

Side effects and CI use

  • serverless prune deletes function versions, layer versions and sandbox image versions in AWS, and with --includeArtifacts deletes objects in the deployment bucket. Without --includeLayers, re-running it with the same --number deletes nothing more until new versions are published. With --includeLayers, a re-run can delete layer versions that the previous run kept (see Layer versions); preview each run with --dryRun --verbose.
  • The command does not prompt, so it runs unattended in CI. Run it with --dryRun --verbose first to see the candidates.
  • Pruning functions calls lambda:ListVersionsByFunction, lambda:ListAliases and lambda:DeleteFunction; pruning layers calls lambda:ListLayerVersions and lambda:DeleteLayerVersion.

Each step prints a line when it finishes:

✔ Pruning of functions complete
✔ Pruning of sandbox image versions complete

With --verbose, the command also prints each version it deletes, such as Deleting function version my-service-dev-hello:2.; with --dryRun --verbose, each candidate, such as my-service-dev-hello:2 selected for deletion.

Provided lifecycle events

  • prune:prune

Examples

Keep the 3 newest versions of every function

serverless prune -n 3

Preview what a prune run would delete

serverless prune -n 3 --dryRun --verbose

Prune one function

serverless prune -n 5 -f myFunction

Prune functions and layers

serverless prune -n 3 --includeLayers

Prune one layer

serverless prune -n 3 -l myLayer

Prune the image versions of one sandbox

serverless prune -n 3 --sandbox mySandbox

Also delete unused deployment artifacts

serverless prune -n 3 --includeArtifacts

Related commands

  • serverless deploy list functions lists the versions of each function.
  • serverless rollback function restores a function's code from one of its versions.
  • serverless rollback restores a whole earlier deployment.
Edit this page
Prev printNextreconcile

Contents

  • serverless prune
  • Options
  • What serverless prune deletes
  • Layer versions
  • Sandbox image versions
  • Deployment artifacts
  • Side effects and CI use
  • Provided lifecycle events
  • Examples
  • Keep the 3 newest versions of every function
  • Preview what a prune run would delete
  • Prune one function
  • Prune functions and layers
  • Prune one layer
  • Prune the image versions of one sandbox
  • Also delete unused deployment artifacts
  • Related commands

Related

GuidesPluginsExamplesSlack CommunitySupport