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:
| Option | Alias | Value | Default | Required | Description |
|---|---|---|---|---|---|
--number | -n | number | Yes | Number of versions to keep besides $LATEST, aliased versions and a sandbox's live version. | |
--function | -f | string | All functions | No | Prune only this function, as named under functions. Sandboxes are left alone. |
--layer | -l | string | No | Prune only this layer, as named under layers. Without --includeLayers, ignored when --function is set. | |
--sandbox | string | No | Prune only this sandbox's image versions, as named under sandboxes. | ||
--includeLayers | -i | boolean | false | No | Also prune layer versions. See What serverless prune deletes for each combination. |
--includeArtifacts | -a | boolean | custom.prune.includeArtifacts, else false | No | Also delete unused deployment artifacts. See Deployment artifacts. Skipped on scoped runs. |
--dryRun | -d | boolean | false | No | Delete nothing. With --verbose, list what would be deleted. |
--verbose | boolean | false | No | Print each version deleted, kept or selected. | |
--stage | -s | string | provider.stage, else dev | No | Stage to prune. |
--region | -r | string | provider.region, else us-east-1 | No | AWS Region of the stage. |
--aws-profile | string | provider.profile | No | AWS profile whose credentials the command uses. | |
--config | -c | file path | serverless.yml | No | Service 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:
| Options | Pruned |
|---|---|
| None | Every function, and every sandbox's image versions. |
--includeLayers | Every function and every layer, and every sandbox's image versions. |
--function | That function. |
--function with --includeLayers | That function and every layer. |
--layer | That layer. |
--layer with --includeLayers | That layer and every function. |
--function with --layer | That function; --layer is ignored. |
--function, --layer and --includeLayers | That function and that layer. |
--sandbox | That sandbox's image versions; --includeLayers is ignored. |
--sandbox with --function or --layer | What 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 prunedeletes function versions, layer versions and sandbox image versions in AWS, and with--includeArtifactsdeletes objects in the deployment bucket. Without--includeLayers, re-running it with the same--numberdeletes 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 --verbosefirst to see the candidates. - Pruning functions calls
lambda:ListVersionsByFunction,lambda:ListAliasesandlambda:DeleteFunction; pruning layers callslambda:ListLayerVersionsandlambda: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 functionslists the versions of each function.serverless rollback functionrestores a function's code from one of its versions.serverless rollbackrestores a whole earlier deployment.