---
title: Object versioning and recycle bin
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

## Versioning

### Creating a new version of an object

```csharp

// Gets an object (CSS stylesheet in this case)
CssStylesheetInfo newStylesheetVersion = CssStylesheetInfoProvider.GetCssStylesheetInfo("Stylesheet");
if (newStylesheetVersion != null)
{
    // Checks if object versioning is allowed for stylesheet objects on the current site
    if (ObjectVersionManager.AllowObjectVersioning(newStylesheetVersion))
    {
        // Sets the properties for the stylesheet version
        newStylesheetVersion.StylesheetDisplayName = newStylesheetVersion.StylesheetDisplayName.ToLowerCSafe();

        // Adds the version to the history of the stylesheet (does not affect the current version)
        ObjectVersionManager.CreateVersion(newStylesheetVersion, CMSActionContext.CurrentUser.UserID, true);
    }
}

```

[> Back to list of examples](#objectversioningandrecyclebin-toc)

### Rolling an object back to a previous version

```csharp

// Gets an object (CSS stylesheet in this case)
CssStylesheetInfo stylesheet = CssStylesheetInfoProvider.GetCssStylesheetInfo("Stylesheet");
if (stylesheet != null)
{
    // Gets version "1.1" of the given stylesheet
    ObjectVersionHistoryInfo version = ObjectVersionHistoryInfoProvider.GetVersionHistories()
                                                                            .WhereEquals("VersionObjectID", stylesheet.StylesheetID)                                                                                            
                                                                            .WhereEquals("VersionObjectType", stylesheet.TypeInfo.ObjectType)
                                                                            .WhereEquals("VersionNumber", "1.1")
                                                                            .FirstObject;

    if (version != null)
    {
        // Rolls the stylesheet back to version 1.1
        ObjectVersionManager.RollbackVersion(version.VersionID);
    }
}

```

[> Back to list of examples](#objectversioningandrecyclebin-toc)

### Permanently deleting an object version

```csharp

// Gets an object (CSS stylesheet in this case)
CssStylesheetInfo stylesheet = CssStylesheetInfoProvider.GetCssStylesheetInfo("Stylesheet");
if (stylesheet != null)
{
    // Gets the latest version of the object
    ObjectVersionHistoryInfo version = ObjectVersionManager.GetLatestVersion(stylesheet.TypeInfo.ObjectType, stylesheet.StylesheetID);

    if (version != null)
    {
        // Permanently deletes the latest version of the object
        ObjectVersionManager.DestroyObjectVersion(version.VersionID);
    }
}

```

[> Back to list of examples](#objectversioningandrecyclebin-toc)

### Clearing the version history of an object

```csharp

// Gets an object (CSS stylesheet in this case)
CssStylesheetInfo stylesheet = CssStylesheetInfoProvider.GetCssStylesheetInfo("Stylesheet");
if (stylesheet != null)
{
    // Clears the version history for the specified object
    ObjectVersionManager.DestroyObjectHistory(stylesheet.TypeInfo.ObjectType, stylesheet.StylesheetID);
}

```

[> Back to list of examples](#objectversioningandrecyclebin-toc)

## Recycle bin

### Deleting an object to the recycle bin

```csharp

// Gets the object (CSS stylesheet in this case)
CssStylesheetInfo deleteStylesheet = CssStylesheetInfoProvider.GetCssStylesheetInfo("Stylesheet");

if (deleteStylesheet != null)
{
    // Checks if the recycle bin is enabled for objects of the given type on the current site
    if (ObjectVersionManager.AllowObjectRestore(deleteStylesheet))
    {
        // Deletes the object (to the recycle bin)
        CssStylesheetInfoProvider.DeleteCssStylesheetInfo(deleteStylesheet);
    }
}

```

[> Back to list of examples](#objectversioningandrecyclebin-toc)

### Permanently deleting (destroying) an object

```csharp

// Gets the object (CSS stylesheet in this case)
CssStylesheetInfo stylesheet = CssStylesheetInfoProvider.GetCssStylesheetInfo("Stylesheet");
if (stylesheet != null)
{
    // Prepares an action context for permanently deleting objects
    using (CMSActionContext context = new CMSActionContext())
    {
        // Disables creation of object versions for all code wrapped within the context (including recycle bin versions)
        context.CreateVersion = false;

        // Permanently deletes (destroys) the object, without creating any records in the recycle bin
        CssStylesheetInfoProvider.DeleteCssStylesheetInfo(stylesheet);
    }
}

```

[> Back to list of examples](#objectversioningandrecyclebin-toc)

### Restoring objects from the recycle bin

```csharp

// Gets all CSS stylesheets from the recycle bin that were deleted by the current user
var deletedStylesheetVersions = ObjectVersionHistoryInfoProvider.GetVersionHistories()
                                        .WhereEquals("VersionObjectType", CssStylesheetInfo.OBJECT_TYPE)
                                        .WhereEquals("VersionDeletedByUserID", MembershipContext.AuthenticatedUser.UserID)
                                        .OrderBy("VersionDeletedWhen DESC");

// Loops through individual deleted stylesheet objects
foreach (ObjectVersionHistoryInfo stylesheetVersion in deletedStylesheetVersions)
{
    // Restores the object from the recycle bin, including any child objects
    ObjectVersionManager.RestoreObject(stylesheetVersion.VersionID, true);
}

```

[> Back to list of examples](#objectversioningandrecyclebin-toc)
